import os
import socket
import threading
import time
import tkinter as tk
import tkinter.messagebox
from io import BytesIO

import customtkinter
import pafy
import pyperclip
import vlc
from PIL import ImageTk, Image
from pyngrok import ngrok
from pytube import Playlist
from pytube import YouTube
from requests import get

global Video_info
Video_info = ["None", "False", "Video_time_sek"]
global sock
global Hoster
global vra
Hoster = 0

customtkinter.set_appearance_mode("dark")
customtkinter.set_default_color_theme("sallify.json")
print(os.path.dirname(os.path.abspath(__file__)))

global False_Call
def play_song(url, volume):
    global Video_info
    Video_info[0] = url
    global player
    global video
    video = pafy.new(url)
    best = video.getbestaudio()
    play_url = best.url
    instance = vlc.Instance()
    player = instance.media_player_new()
    media = instance.media_new(play_url)
    media.get_mrl()
    player.set_media(media)
    player.play()
    player.audio_set_volume(int(volume))

def start_server():
    global sock
    ngrok.set_auth_token("2AYWrd8D7u9WAhpit1cRzCclzt4_47YboKYHbaAFqx2VvbQbU")
    host = socket.gethostname()
    port = 1509

    # Create a TCP socket

    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

    # Bind a local socket to the port
    server_address = ("", port)
    sock.bind(server_address)
    sock.listen(1)

    # Open a ngrok tunnel to the socket
    public_url = ngrok.connect(port, "tcp", options={"remote_addr": "{}:{}".format(host, port)})
    print("ngrok tunnel \"{}\" -> \"tcp://127.0.0.1:{}/\"".format(public_url, port))
    global code
    code = str(public_url)[20] + str(public_url)[34:40]

    global Video_info

    while True:
        connection = None
        try:
            # Wait for a connection
            print("\nWaiting for a connection ...")
            connection, client_address = sock.accept()

            print("... connection established from {}".format(client_address))

            # Receive the message, send a response
            while True:
                data = connection.recv(1024)
                if data:
                    Request = data.decode("utf-8")
                    print("Sending: {}".format(str(Video_info)))
                    connection.sendall(str(Video_info).encode("utf-8"))
                else:
                    break
        except KeyboardInterrupt:
            print(" Shutting down server.")

            if connection:
                connection.close()
            break

    sock.close()

"""with open('Playlists.txt', 'rb') as f:
    Songs = load(f)"""
Songs_Full = []
Songs = []
url = "https://youtu.be/1VD17MgCMhM"
video = YouTube(url)
Songs_Half = video.description
for i in Songs_Half.split(","):
    Songs.append(i.split("¦"))

root = customtkinter.CTk()
root.title('Sallify')
root.geometry("1000x500+50+50")
root.iconbitmap("Assets/Sqaure.ico")
Icon = ImageTk.PhotoImage(Image.open("Assets/Green.png"))
panel = tk.Label(root, image=Icon, bg="black")
Ican = ImageTk.PhotoImage(Image.open("Assets/Reload_white.png"))
img = []

def load_songs():
    try:
        for i in Songs:
            p = Playlist(i[1])
            y = YouTube(p.video_urls[0])
            url = y.thumbnail_url
            response = get(url)
            img_data = response.content
            img.append(ImageTk.PhotoImage(Image.open(BytesIO(img_data))))
    except:
        tkinter.messagebox.showerror(root, title="Conection error",message="Please make wifi good and restart")

load_s = threading.Thread(target=load_songs)
load_s.start()

def showimg(e):
    a = lst.curselection()
    fname = lst.get(a)
    for i in range(len(Songs)):
        if Songs[i][0] == fname:
            try:
                panel.configure(image=img[i])
                panel.pack(side="bottom", fill="both", expand="yes")
                panel.update()
            except:
                panel.configure(image=Ican)
                panel.pack(side="bottom", fill="both", expand="yes")
                panel.update()

def Start_playlist():
    subroot =  customtkinter.CTk()
    subroot.geometry("490x340")
    subroot.grid_columnconfigure(0, weight=1)
    subroot.grid_rowconfigure(0, weight=1)
    playlist = tk.Listbox(subroot, selectmode=tk.SINGLE, bg="black", fg="white", width=40)
    playlist.config(font=('Roboto', 15))
    playlist.grid(columnspan=10, sticky="news")
    Z = lst.curselection()
    Aname = lst.get(Z)
    def threaded_Loading():
        for e in range(len(Songs)):
            if Songs[e][0] == Aname:
                p = Playlist(Songs[e][1])
                print(p.title)
                for Url in p.video_urls:
                    Name = YouTube(Url).title
                    playlist.insert(tk.END, Name+" "*1000+","+Url)

    a = threading.Thread(target=threaded_Loading)
    a.start()

    def test(e):
        print("select")
    playlist.bind("<<ListboxSelect>>", test)

    def Load_Song(Link):
        global Video_info
        Video_info[0] = Link
        try:
            player.stop()
        except:
            pass
        play_song(Link, Volumeslider.get())
        Timeslider.config(from_=0, to=video.length)
        Timeslider.set(0)
        z = threading.Thread(target=Has_ended)
        z.start()

    def Has_ended():
        global Video_info
        global False_Call
        while True:
            False_Call = True
            global Video_info
            Timeslider.set(video.length*(player.get_position()))
            Video_info[2] = round(video.length*(player.get_position()))
            time.sleep(5)
            try:
                if str(player.get_state()) == "State.Ended":
                    Next_int = -1
                    print(player.get_state())
                    while True:
                        Next_int += 1
                        if video.title in str(playlist.get(Next_int)):
                            try:
                                print((playlist.get(Next_int+1)).split(",")[0])
                                Load_Song((playlist.get(Next_int+1)).split(",")[1])
                                break
                            except:
                                Load_Song((playlist.get(0)).split(",")[1])
                                break
                                print("First song")
                    break
                else:

                    print(str(player.get_state()))
            except NameError as e:
                print(e)

    def playSong():
        try:
            player.stop()
        except:
            pass
        Puase_Resumebtn.config(text="  ⏸ ")
        pay = playlist.curselection()
        gayname = playlist.get(pay)
        play_song(gayname.split(",")[1], Volumeslider.get())
        Timeslider.config(from_=0, to=video.length)
        Timeslider.set(0)
        z = threading.Thread(target=Has_ended)
        z.start()
        Volumeslider.set(player.audio_get_volume())

    def Puase_ResumeSong():
        print(player.is_playing())
        if player.is_playing() == 1:
            player.pause()
            Video_info[1] = "True"
            Puase_Resumebtn.config(text="  ▶ ")
        elif player.is_playing() == 0:
            Video_info[1] = "False"
            Puase_Resumebtn.config(text="  ⏸ ")
            player.pause()

    def Change_volume(e):
        try:
            text = ("Volume : "+str(round(e)))
            player.audio_set_volume(int(e)*5)
            VolumeEntry.delete(0,tk.END)
            VolumeEntry.insert(0,text)
        except:
            pass

    def Change_Loacation(e):
        global False_Call
        try:
            if False_Call:
                Video_info[2] = e
                False_Call = False
            elif not False_Call:
                Video_info[2] = e
                player.set_time(int(e)*1000)
                False_Call = False
        except:
            pass

    def Enterred(e):
        try:
            Text = int(VolumeEntry.get())
            if Text > 0 and Text < 200:
                Volumeslider.set(Text)
        except:
            text = ("Volume : " + str(round(player.audio_get_volume()/5)))
            VolumeEntry.delete(0, tk.END)
            VolumeEntry.insert(0, text)

    def CLicked(e):
        VolumeEntry.configure(state=tk.NORMAL)
        VolumeEntry.delete(0, tk.END)

    subroot.title('Sallify')
    subroot.iconbitmap("Assets/Sqaure.ico")
    songstatus = tk.StringVar()
    songstatus.set("choosing")
    # playlist---------------

    playbtn = customtkinter.CTkButton(subroot, text="play", command=playSong, text_font=('Roboto', 20))
    # playbtn.config(font=('arial', 20), bg="black", fg="white", padx=70, pady=6)
    playbtn.grid(row=1, rowspan=2, column=0, sticky="news")
    Puase_Resumebtn = customtkinter.CTkButton(subroot, text="  ⏸ ", command=Puase_ResumeSong)
    # Puase_Resumebtn.config(font=('arial', 20), bg="black", fg="white", padx=7, pady=6)
    Puase_Resumebtn.grid(row=1, rowspan=2, column=1, sticky="NSEW")
    # Canvy = customtkinter.CTkFrame(subroot)
    # Canvy.grid(row=1, column=2, sticky="ew", )
    VolumeEntry = customtkinter.CTkEntry(subroot,placeholder_text="Volume")
    VolumeEntry.config()
    # Volumeslider.config(font=('arial', 20), bg="black", fg="white",length=150)
    VolumeEntry.grid(row=1, column=2)
    Volumeslider = customtkinter.CTkSlider(subroot, from_=1,to=100, orient='horizontal',command=Change_volume)
    Volumeslider.set(20)
    # Volumeslider.config(font=('arial', 20), bg="black", fg="white",length=150)
    Volumeslider.grid(row=2, column=2)
    Timeslider = customtkinter.CTkSlider(subroot, from_=0,to=100, orient='horizontal',command=Change_Loacation)
    # Timeslider.config(font=('arial', 20), bg="gray", fg="white",length=450)
    Timeslider.grid(row=3, column=0,columnspan=4, sticky="NSEW")
    VolumeEntry.bind('<Button-1>', CLicked)
    VolumeEntry.bind('<Return>', Enterred)
    subroot.mainloop()

def start_threaded_playlist():
    # x =threading.Thread(target=Start_playlist)
    # x.start()
    Start_playlist()
def client_listen_along(Code):
    while True:
        time.sleep(1)
        Full_info = Code.split(":")
        host = f"{Full_info[0]}.tcp.ngrok.io"
        port = str(Full_info[1])

        sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

        server_address = (host, port)
        sock.connect(server_address)
        print("Connected to {}:{}".format(host, port))

        message = "Video"
        print("Sending: {}".format(message))
        sock.sendall(message.encode("utf-8"))

        data_received = 0
        data_expected = len(message)

        while data_received < data_expected:
            data = sock.recv(1024)
            data_received += len(data)
            print(data.decode("utf-8"))
            sock.close()
        sock.close()
def Client_setup():
    Host_b.pack_forget()
    Join_b.pack_forget()
    Ask_code.pack(side="right", fill=tk.X)
    start.pack(side="right", fill=tk.X)
    Filan_audio_bar.pack(side="right", fill=tk.X)
def start_threaded_listen_along():
    if stream_Buton.get():
        tkinter.messagebox.showinfo(message="Please full screen window to see full gui")
        Host_b.pack(side="right", fill=tk.X)
        Join_b.pack(side="right", fill=tk.X)
    else:
        vra = False
        Filan_audio_bar.pack_forget()
        Host_b.pack_forget()
        Join_b.pack_forget()
        stop.pack_forget()
        Ask_code.pack_forget()
        copyCode.pack_forget()
        start.pack_forget()
        ngrok.kill()
def start_server_setup():
    server = threading.Thread(target=start_server)
    server.start()
    Host_b.pack_forget()
    Join_b.pack_forget()
    stop.pack(side="right", fill=tk.X)
    copyCode.pack(side="right", fill=tk.X)
def stop_server():
    ngrok.kill()
    global vra
    vra = False
    stream_Buton.toggle()

global old_hoho
old_hoho = Video_info
old_hoho[1] == "False"
global vra
vra = False
def vra_serber_vir_informasie():
    global old_hoho
    while vra:
        print(vra)
        time.sleep(1)
        gagagagaag = Ask_code.get().split(":")
        host = f"{gagagagaag[0]}.tcp.ngrok.io"
        port = int(gagagagaag[1])
        pock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        server_address = (host, port)
        pock.connect(server_address)
        message = "Video"
        pock.sendall(message.encode("utf-8"))
        data_received = 0
        data_expected = len(message)
        while data_received < data_expected:
            data = pock.recv(1024)
            data_received += len(data)
            time.sleep(2)
            hohoho = data.decode("utf-8")
            hohoho = hohoho.replace(" ", "").replace("'", "").replace("[", "").replace("]", "")
            hohoho = hohoho.split(",")
            if hohoho != old_hoho:
                if hohoho[0] != old_hoho[0]:
                    try:
                        player.stop()
                    except:
                        pass
                    play_song(hohoho[0], Filan_audio_bar.get())
                    old_hoho = hohoho
                if hohoho[1] != old_hoho[1]:
                    print(str(hohoho) + " Recieved")
                    print(str(old_hoho) + " had")
                    old_hoho = hohoho
                    if hohoho[1] == "True":
                        print("pausing")
                        if player.is_playing() == 1:
                            player.pause()
                    elif hohoho[1] == "False":
                        if player.is_playing() == 0:
                            print("playing")
                            player.pause()
                if hohoho[2] != old_hoho[2]:
                    if int(hohoho[2]) - 5 > round(video.length*(player.get_position())):
                        try:
                            player.set_time(int(hohoho[2])*1000)
                        except:
                            pass
                    elif int(hohoho[2]) + 5 < round(video.length*(player.get_position())):
                        try:
                            player.set_time(int(hohoho[2]) * 1000)
                        except:
                            pass
                    old_hoho = hohoho
            pock.close()
        pock.close()

def join_server():
    global vra
    print("Joining")
    vra = True
    client = threading.Thread(target=vra_serber_vir_informasie)
    client.start()
def Copy_code():
    global code
    pyperclip.copy(code)
def Final_audio_slider(e):
    try:
        player.audio_set_volume(int(e))
    except:
        pass

def on_closing():
    print("closing")
    ngrok.kill()
    try:
        player.stop()
    except:
        pass
    root.destroy()
    quit()
    quit()

root.protocol("WM_DELETE_WINDOW", on_closing)

Filan_audio_bar = customtkinter.CTkSlider(from_=1,to=100, orient='horizontal', command=Final_audio_slider)
subbie = customtkinter.CTk()
copyCode = customtkinter.CTkButton(text="Copy code", command=Copy_code)
stop = customtkinter.CTkButton(text="Stop", command=stop_server)
start = customtkinter.CTkButton(text="Join", command=join_server)
Ask_code = customtkinter.CTkEntry(placeholder_text="Paste Code here")
Host_b = customtkinter.CTkButton(text="Host listen along", command=start_server_setup)
Join_b = customtkinter.CTkButton(text="Join listen along", command=Client_setup)
stream_Buton = customtkinter.CTkSwitch(text="Listen along",command=start_threaded_listen_along )
stream_Buton.pack(side="top",fill=tk.X)
b1 = customtkinter.CTkButton(text="Play",command=start_threaded_playlist)
b1.pack(side="bottom",fill=tk.X)
lst = tk.Listbox(root, bg="black", fg="white")
lst.pack(side="left",fill="both", ipadx=20)

for fname in Songs:
    lst.insert(tk.END, fname[0])
lst.config(font=('Roboto', 12))
lst.bind("<<ListboxSelect>>", showimg)
panel.pack(side="bottom", fill="both", expand="yes", ipadx=10)
# panel.pack(side="bottom", fill="both", expand="yes")
print("done")

root.mainloop()


import base64
import io
import os

from PIL import Image, ImageDraw, ImageFont
import requests

class WelcomeCard:
    BASE_FONT = ImageFont.truetype(
        os.path.abspath("ether/assets/fonts/Inter-Medium.ttf"), 16
    )
    WELCOME_FONT = ImageFont.truetype(
        os.path.abspath("ether/assets/fonts/Inter-Bold.ttf"), 24
    )
    MASK = Image.open("ether/assets/mask.png", "r").convert("L").resize((100, 100))

    @classmethod
    def create_card(cls, user, guild):
        img = Image.new("RGBA", (400, 200), (231, 228, 220))

        # Profile Picture
        r = requests.get(user.display_avatar.with_size(256).url)
        pp = Image.open(io.BytesIO(r.content))
        pp = pp.resize((100, 100))

        img.paste(pp, (150, 25), cls.MASK)

        draw = ImageDraw.Draw(img)

        # Welcome Text
        draw.text(
            xy=(200, 140),
            text="Welcome!",
            fill=(64, 64, 64),
            font=cls.WELCOME_FONT,
            anchor="ma",
        )

        # Name
        draw.text(
            xy=(200, 170),
            text=f"{user.display_name[:20]}",
            fill=(64, 64, 64),
            font=cls.BASE_FONT,
            anchor="ma",
        )

        # Convert to Base64
        with io.BytesIO() as buffer:
            img.save(buffer, format="PNG", quality=75)
            buffer.seek(0)
            result = base64.b64encode(buffer.getvalue()).decode()
            return io.BytesIO(base64.b64decode(result))


import requests
import urllib.parse as urlparse
import json,sys
from pprint import pprint

isErrorbased = ['"', "'", '--']
isJson = []

CYELL = '\033[1;93m'
CENDYELL = '\033[0m'
CGRE = '\033[1;92m'
CYAN = '\033[1;36m'
RED =  '\033[1;31m'

class SimpleSqlCheck():

    def __init__(self, isUrl, isLocation):
        self.isUrl = isUrl
        self.isLocation = isLocation

    def ParsingJson(self):
        if (self.isLocation == False):
            print (RED,"[WARNING] Please add specifict file json location example -j /home/user/api.json",CENDYELL)
            sys.exit(0)
        else :
            print ("[INFO] Parsing Json where method GET ..")
            try:
                with open(self.isLocation) as f:
                    data = json.load(f)
                    isItems = data['item']
                    for items in isItems:
                        try:
                            item = items['item']
                        except (TypeError, KeyError):
                            pass

                        for i in item:
                            try:
                                isMethod = i['request']['method']
                                if (isMethod == 'GET'):
                                    isJson.append(i)
                            except (TypeError, KeyError):
                                pass
            except (FileNotFoundError) :
                print (RED,"[WARNING] Please add specifict file json location example -j /home/user/api.json ", CENDYELL)
                sys.exit(0)

    def isCheckNormalQuery(self):
        try:
            isHeaders = { 'userToken' : 'null'}
            isStatus = (requests.get(self.isUrl, params=isHeaders , timeout=10))
            if (isStatus.status_code == 200):
                print ("[INFO] status OK")
                return isStatus.headers['Content-Length']

        except Exception as e:
                print (RED,"[WARNING] Oops Request timeout ",e, CENDYELL)
                sys.exit(0)

    def isParsingUrl(self):
        print (CYAN,"[INFO] is Parsing url ..", CENDYELL)
        # pprint (isJson)
        print (CYAN,"[INFO] Is url detected ..", CENDYELL)
        for isurl in isJson:
            print (isurl['request']['url']['raw'])

    def IsQueryErrors(self, isContentLength):
        print ("[INFO] Checking Error Based ..")
        # isQueryParsed = urlparse.urlparse(self.isUrl).query
        # isQuery = isQueryParsed.split('&')
        # print(isQuery)

        parsed = urlparse.urlparse(self.isUrl).query
        print (urlparse.parse_qs(parsed))
        # querys = parsed.query.split("&")
        # result = []
        # for query in querys:
        #     for pairs in isErrorbased :
        #         print (query)
        #         new_query = "&".join([ "{}{}".format(query, pairs)])
        #         print (new_query)
        #         parsed = parsed._replace(query=new_query)
        #         result.append(urlparse.urlunparse(parsed))
        # print (result)

    def CheckSqlInjection(self):
        if (self.isLocation != False):
            self.ParsingJson()
            self.isParsingUrl()

        else:
            lenght = self.isCheckNormalQuery()
            self.IsQueryErrors(lenght)


from django.shortcuts import render,get_object_or_404
from django.http import HttpResponse
# Create your views here.
from .serializer import CustomerSerializer, ProductSerializer, SubscriptionSerializer
from .models import Customer, Product, Subscription
from rest_framework import status
from rest_framework.decorators import api_view
from rest_framework.response import Response

@api_view(['GET', 'POST'])
def subscription_list(request):
    """
    List all code snippets, or create a new snippet.
    """
    if request.method == 'GET':
        subscription = Subscription.objects.all()
        serializer = SubscriptionSerializer(subscription, many=True)
        return Response(serializer.data)

    elif request.method == 'POST':
        serializer = SubscriptionSerializer(data=request.data)
        if serializer.is_valid():
            serializer.save()
            return Response(serializer.data, status=status.HTTP_201_CREATED)
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

@api_view(['GET', 'PATCH'])
def subscription_detail(request, pk):
    """
    Retrieve, update or delete a code snippet.
    """
    try:
        snippet = Subscription.objects.get(pk=pk)
    except Subscription.DoesNotExist:
        return Response(status=status.HTTP_404_NOT_FOUND)

    if request.method == 'GET':
        serializer = Subscription(snippet)
        return Response(serializer.data)

    elif request.method == 'PATCH':
        serializer = SubscriptionSerializer(snippet, data=request.data)
        if serializer.is_valid():
            serializer.save()
            return Response(serializer.data)
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

@api_view(['GET', 'POST'])
def customer_list(request):
    """
    List all code snippets, or create a new snippet.
    """
    if request.method == 'GET':
        customer = Customer.objects.all()
        serializer = CustomerSerializer(customer, many=True)
        return Response(serializer.data)

    elif request.method == 'POST':
        serializer = CustomerSerializer(data=request.data)
        if serializer.is_valid():
            serializer.save()
            return Response(serializer.data, status=status.HTTP_201_CREATED)
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

@api_view(['GET', 'POST'])
def product_list(request):
    """
    List all code snippets, or create a new snippet.
    """
    if request.method == 'GET':
        products = Product.objects.all()
        serializer = ProductSerializer(products, many=True)
        return Response(serializer.data)

    elif request.method == 'POST':
        serializer = ProductSerializer(data=request.data)
        if serializer.is_valid():
            serializer.save()
            return Response(serializer.data, status=status.HTTP_201_CREATED)
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

@api_view(['GET'])
def subscription_check(request,customer_id, product_name):
    """
    List all code snippets, or create a new snippet.
    """
    if request.method == 'GET':
        subscription_check = get_object_or_404(Subscription,customer_id=customer_id, product_name= product_name)
        serializer = SubscriptionSerializer(subscription_check)
        return Response(serializer.data)


import os, os.path
import string
import cherrypy
import datetime
import requests
import lxml
import cssselect
import lxml.html
import json
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
#from email.MIMEBase import MIMEBase
from pprint import pprint

class Data(object):
    def __init__(self,title,description,link):
        self.title = title
        self.description = description
        self.link = link
class SendMail(object):
    def __init__(self):
        print('envoi de mail')
    def methodetest(self):
        print('je suis dans la méthode test')

class MonSiteWeb(object):

    global htmlsite
    global variable
    global tabtitle
    global tabdesc
    global tablink
    global tabind
    #global tabdata
    global listededonnee

    def methodetest(self):
        print('je suis dans la méthode test')

    @cherrypy.expose
    def index(self):

        now = datetime.datetime.now()
        entetesite = ''' <!DOCTYPE html>

    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <meta name="description" content="">
    <meta name="author" content="">

    <title>Appel d'offre</title>

    <link href="/static/bootstrap-3.3.7-dist/vendor/bootstrap/css/bootstrap.min.css" rel="stylesheet">

    <link href="/static/bootstrap-3.3.7-dist/css/scrolling-nav.css" rel="stylesheet">

	<link rel="stylesheet" type="text/css" href="/static/bootstrap-3.3.7-dist/DataTables/datatables.min.css"/>

Socitech

        <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarResponsive" aria-controls="navbarResponsive" aria-expanded="false" aria-label="Toggle navigation">

        </button>

              About

              Ajouter un filtre

              Action

 Appel d'offre venant du site global tender

resultat du "data scraping" du site global tender

Resultat de la recherche

			<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModalLong">
				Envoi de la selection
			</button>

Date	Title	Description	Link

'''
        table = "
{}	{}	{}	{}
"

        footer = '''

Contacts

				<button type="button" class="close" data-dismiss="modal" aria-label="Close">
					×
				</button>

Ajoujer un Contact

				<form>

						Name :
						<input type="text" class="form-control" id="name" placeholder="Name"  required>

						Email :
						<input type="email" class="form-control" id="email" placeholder="EMAIL"  required>

						Telephone :
						<input type="tel" class="form-control" id="telephone" placeholder="Telephone"  required>

						<button type="button" class="btn btn-primary" id="addContact">Add Contacts</button>

				</form>

NOMS	EMAIL	NUMERO

Rudy	rudy@yahoo.fr	671402318

				<button id="moveContact" type="button" class="btn btn-primary">move selected row</button>
				<button id="sendEmail" type="button" class="btn btn-primary">send to selected row</button>
				<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>

Copyright © Your Website 2017

'''
        tabtitle = []
        tabdesc = []
        tablink = []
        tabind = []
        global tabdata

        txt = 'ma variable'
        indice = 0
        url = 'https://www.globaltenders.com/tenders-cameroon.php'
        while(url !='#'):
            reponse = requests.get(url)#'http://www.globaltenders.com/'
            tree = lxml.html.fromstring(reponse.text)
            tdElems = tree.xpath('//table/tr/td[@style="width:70%; vertical-align:top; text-align:left;"]')#|//table/tr/td/a/@href
            tdElink = tree.xpath('//table/tr/td/a/@href')
            taille = len(tdElems)
            indice = 0
            intitule =''
            adresse = ''
            mylist = list()
            mydescription = list()
            mytitle = list()
            listData = list()
            while (indice < taille):
                if(tdElems[indice].text_content() == ' Technology Hardware and Equipment'):
                    ltail = indice//2
                    intitule = ' Technology Hardware and Equipment'
                    adresse = tdElink[ltail]
                    mylist.append(adresse)
                    val = indice+1
                    mydescription.append(tdElems[val].text_content())
                    mytitle.append(tdElems[indice].text_content())
                    data = Data(tdElems[indice].text_content(),tdElems[val].text_content(),adresse)
                    listData.append(data)
                    print(tdElems[indice].text_content())
                    #print(adresse)
                elif(tdElems[indice].text_content() == ' Smart Cards and other Access Control system , Printing and publishing'):

                    ltailt = indice//2
                    intitulet = tdElems[indice].text_content()
                    adresset = tdElink[ltailt]
                    mylist.append(adresset)
                    val = indice+1
                    mydescription.append(tdElems[val].text_content())
                    mytitle.append(tdElems[indice].text_content())
                    data = Data(tdElems[indice].text_content(),tdElems[val].text_content(),adresset)
                    listData.append(data)
                    print(tdElems[indice].text_content())
                    #print(adresset)

                elif(tdElems[indice].text_content() == ' Software Services'):

                    ltailf = indice//2
                    intitulef = tdElems[indice].text_content()
                    adressef = tdElink[ltailf]
                    mylist.append(adressef)
                    val = indice+1
                    mydescription.append(tdElems[val].text_content())
                    mytitle.append(tdElems[indice].text_content())
                    data = Data(tdElems[indice].text_content(),tdElems[val].text_content(),adressef)
                    listData.append(data)
                    print(tdElems[indice].text_content())
                    #print(adressef)

                elif(tdElems[indice].text_content() == ' Bridges and Tunnels'):

                    ltails = indice//2
                    intitules = tdElems[indice].text_content()
                    adresses = tdElink[ltails]
                    mylist.append(adresses)
                    val = indice+1
                    mydescription.append(tdElems[val].text_content())
                    mytitle.append(tdElems[indice].text_content())
                    data = Data(tdElems[indice].text_content(),tdElems[val].text_content(),adresses)
                    listData.append(data)
                    print(tdElems[indice].text_content())
                    #print(adresses)

                elif(tdElems[indice].text_content() == ' Services'):

                    ltailn = indice//2
                    intitulen = tdElems[indice].text_content()
                    adressen = tdElink[ltailn]
                    mylist.append(adressen)
                    val = indice+1
                    mydescription.append(tdElems[val].text_content())
                    mytitle.append(tdElems[indice].text_content())
                    data = Data(tdElems[indice].text_content(),tdElems[val].text_content(),adressen)
                    listData.append(data)
                    print(tdElems[indice].text_content())
                    #print(adressen)

                elif(tdElems[indice].text_content() == ' Printing and publishing'):

                    ltailk = indice//2
                    intitulek = tdElems[indice].text_content()
                    adressek = tdElink[ltailk]
                    mylist.append(adressek)
                    mydescription.append(tdElems[indice+1].text_content())
                    mytitle.append(tdElems[indice].text_content())
                    data = Data(tdElems[indice].text_content(),tdElems[indice+1].text_content(),adressek)
                    listData.append(data)
                    print(tdElems[indice].text_content())
                    #print(adressek)

                elif(tdElems[indice].text_content() == ' Industry , Technology Hardware and Equipment , Furniture'):

                    ltaily = indice//2
                    intituley = tdElems[indice].text_content()
                    adressey = tdElink[ltaily]
                    mylist.append(adressey)
                    mydescription.append(tdElems[indice+1].text_content())
                    mytitle.append(tdElems[indice].text_content())
                    data = Data(tdElems[indice].text_content(),tdElems[indice+1].text_content(),adressey)
                    listData.append(data)
                    print(tdElems[indice].text_content())
                    #print(adressey)
                elif(tdElems[indice].text_content() == ' Telecommunications , Information Technology (IT) , Consultancy , Services , Infrastructure and construction'):
                    ltaill = indice//2
                    intitulel = tdElems[indice].text_content()
                    adressel = tdElink[ltaill]
                    mylist.append(adressel)
                    mydescription.append(tdElems[indice+1].text_content())
                    mytitle.append(tdElems[indice].text_content())
                    data = Data(tdElems[indice].text_content(),tdElems[indice+1].text_content(),adressel)
                    listData.append(data)
                    print(tdElems[indice].text_content())
                    #print('*****************')

                elif(tdElems[indice].text_content() == ' Industry , Technology Hardware and Equipment'):

                    ltailycp = indice//2
                    intituleycp = tdElems[indice].text_content()
                    adresseycp = tdElink[ltailycp]
                    mylist.append(adresseycp)
                    mydescription.append(tdElems[indice+1].text_content())
                    mytitle.append(tdElems[indice].text_content())
                    data = Data(tdElems[indice].text_content(),tdElems[indice+1].text_content(),adresseycp)
                    listData.append(data)
                    print(tdElems[indice].text_content())

                elif(tdElems[indice].text_content() == ' Telecommunications'):

                    ltailytele = indice//2
                    intituleytele = tdElems[indice].text_content()
                    adresseytele = tdElink[ltailytele]
                    mylist.append(adresseytele)
                    mydescription.append(tdElems[indice+1].text_content())
                    mytitle.append(tdElems[indice].text_content())
                    data = Data(tdElems[indice].text_content(),tdElems[indice+1].text_content(),adresseytele)
                    listData.append(data)
                    print(tdElems[indice].text_content())

                elif(tdElems[indice].text_content() == ' Technology Hardware and Equipment , Energy, Power and Electrical'):

                    ltailyHard = indice//2
                    intituleyHard = tdElems[indice].text_content()
                    adresseyHard = tdElink[ltailyHard]
                    mylist.append(adresseyHard)
                    mydescription.append(tdElems[indice+1].text_content())
                    mytitle.append(tdElems[indice].text_content())
                    data = Data(tdElems[indice].text_content(),tdElems[indice+1].text_content(),adresseyHard)
                    listData.append(data)
                    print(tdElems[indice].text_content())

                elif(tdElems[indice].text_content() == ' Telecommunications , Banking, Finance, Insurance and Securities (BFIS) , Information Technology (IT) , Consultancy'):

                    ltailyBank = indice//2
                    intituleyBank = tdElems[indice].text_content()
                    adresseyBank = tdElink[ltailyBank]
                    mylist.append(adresseyBank)
                    mydescription.append(tdElems[indice+1].text_content())
                    mytitle.append(tdElems[indice].text_content())
                    data = Data(tdElems[indice].text_content(),tdElems[indice+1].text_content(),adresseyBank)
                    listData.append(data)
                    print(tdElems[indice].text_content())

                elif(tdElems[indice].text_content() == ' Telecommunications , Information Technology (IT) , Consultancy , Infrastructure and construction , Building'):

                    ltailyInfor = indice//2
                    intituleyInfor = tdElems[indice].text_content()
                    adresseyInfor = tdElink[ltailyInfor]
                    mylist.append(adresseyInfor)
                    mydescription.append(tdElems[indice+1].text_content())
                    mytitle.append(tdElems[indice].text_content())
                    data = Data(tdElems[indice].text_content(),tdElems[indice+1].text_content(),adresseyInfor)
                    listData.append(data)
                    print(tdElems[indice].text_content())

                elif(tdElems[indice].text_content() == ' Telecommunications , Information Technology (IT) , Software Services , Consultancy'):

                    ltailymunic = indice//2
                    intituleymunic = tdElems[indice].text_content()
                    adresseymunic = tdElink[ltailymunic]
                    mylist.append(adresseymunic)
                    mydescription.append(tdElems[indice+1].text_content())
                    mytitle.append(tdElems[indice].text_content())
                    data = Data(tdElems[indice].text_content(),tdElems[indice+1].text_content(),adresseymunic)
                    listData.append(data)
                    print(tdElems[indice].text_content())

                elif(tdElems[indice].text_content() == ' Telecommunications , Infrastructure and construction'):

                    ltailyInfras = indice//2
                    intituleyInfras = tdElems[indice].text_content()
                    adresseyInfras = tdElink[ltailyInfras]
                    mylist.append(adresseyInfras)
                    mydescription.append(tdElems[indice+1].text_content())
                    mytitle.append(tdElems[indice].text_content())
                    data = Data(tdElems[indice].text_content(),tdElems[indice+1].text_content(),adresseyInfras)
                    listData.append(data)
                    print(tdElems[indice].text_content())

                elif(tdElems[indice].text_content() == ' Telecommunications , Information Technology (IT) , Consultancy'):

                    ltailyConsul = indice//2
                    intituleyConsul = tdElems[indice].text_content()
                    adresseyConsul = tdElink[ltailyConsul]
                    mylist.append(adresseyConsul)
                    mydescription.append(tdElems[indice+1].text_content())
                    mytitle.append(tdElems[indice].text_content())
                    data = Data(tdElems[indice].text_content(),tdElems[indice+1].text_content(),adresseyConsul)
                    listData.append(data)
                    print(tdElems[indice].text_content())

                indice = indice + 1
                tabtitle = tabtitle + mytitle
                #tabtitle = mytitle
                tabdesc  = tabdesc + mydescription
                #tabdesc = mydescription
                tablink  = tablink + mylist
                #tablink = mylist
                tabdata = listData

            #tabdata = listData
            #tabind =
            #print(listData,'*********************')
            tdElemslm = tree.xpath('//select[@style="float:left"]')
            mlistlm = tdElemslm[0].getnext().getchildren()
            if len(mlistlm) == 2 :
                #print(mlistlm[1].xpath('@href')[0])
                adressederedirection = mlistlm[1].xpath('@href')[0]
            else :
                #print(mlistlm[0].xpath('@href')[0])
                if(mlistlm[0].text_content() == 'Next Page'):
                    adressederedirection = mlistlm[0].xpath('@href')[0]
                else:
                    adressederedirection = '#'
            url= adressederedirection

        #global listData
        #listData = list()
        listededonnee = list()
        nlisted = list()
        inlist=0
        while inlist < len(tabtitle):
            donnee = Data(tabtitle[inlist],tabdesc[inlist],tablink[inlist])
            listededonnee.append(donnee)
            inlist = inlist+1
        print(listededonnee)
        print(len(listededonnee),'/*-/*-/*-/*-/*-/*-/*-/*-/*-/*-/*-/*-/*-/*-/*-')

        for donnee in listededonnee :
            c = 0
            nindic = 1
            lesindices = []
            listedesdons = listededonnee
            while nindic < len(listededonnee):
                if donnee.title == listededonnee[nindic].title and donnee.description == listededonnee[nindic].description and donnee.link == listededonnee[nindic].link:
                    c = c+1
                    if(c>0):
                       listededonnee.remove(listededonnee[nindic])
                nindic = nindic + 1
            print(c)

        print(listededonnee)
        print(len(listededonnee),'/*-+/*-+/*-+/*-+/*-+/*-+/*-+/*-+/*-+/*-+/*-+/*-+/*-+')

        #listData = listededonnee

        for el in listededonnee :
            inc = 0
            indel = 0
            while indel < len(listededonnee):
                if el.title == listededonnee[indel].title and el.description == listededonnee[indel].description and el.link == listededonnee[indel].link:
                    inc = inc + 1
                    if inc >1 :
                        listededonnee.remove(listededonnee[indel])
                indel = indel + 1
        for iii in listededonnee :
            print(iii.title)

        pagehtml = ''
        for td in listededonnee :
            pagehtml = pagehtml + table.format(str(now),td.title,td.description,td.link,td.link)

        return entetesite + pagehtml + footer

    @cherrypy.expose
    def sendMail(self,donnee,qtdonnee,addresse,qtAddonnee):
        cherrypy.response.headers['Content-Type'] = 'application/json'
        variable = r"""{}"""
        data = json.loads(variable.format(donnee))
        print(data,'+/++/+/++/++/++/++/++/*+/+*/+*/+*/+*/+*/+*/+*/')
        print('---------------------------------------------------')
        indice = 0
        message = """
                    <!DOCTYPE html>

                                <meta charset="utf-8">
                                <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
                                <meta name="description" content="">
                                <meta name="author" content="">
                                <title>Appel d'offre</title>

                                <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css" integrity="sha384-9gVQ4dYFwwWSjIDZnLEWnxCjeSWFphJiwGPXr1jddIhOegiu1FwO5qRGvFXOdJZ4" crossorigin="anonymous">

Title	Description	Link

"""
        tdata = """
{}	{}	lien du site
"""

        fin = """

                    """
        valdonnee = ''
        while indice < int(qtdonnee) :
            chn = str(indice)
            valdonnee = valdonnee + tdata.format(data[chn]['titre'],data[chn]['description'],'http://www.globaltenders.com/auth-tenders.php?action=details&id='+data[chn]['lien'])
            indice = indice + 1

        indicead = 0
        variablead = r"""{}"""
        address = json.loads(variable.format(addresse))
        lAd = list()
        while indicead < int(qtAddonnee) :
           chn = str(indicead)
           valAd = address[chn]['init']+'@'+ address[chn]['end']
           lAd.append(valAd)
           print(valAd)
           indicead = indicead + 1

        MessageSending = message + valdonnee +fin
        indiceMail = 0
        me = 'tekamfossi@gmail.com'
        text = "Appel d'offre !"
        part1 = MIMEText(text, 'plain')
        part2 = MIMEText(MessageSending, 'html')
        msg = MIMEMultipart('alternative')
        msg['Subject'] = "Appel d'offre"
        msg['From'] = me
        msg.attach(part1)
        msg.attach(part2)
        server = smtplib.SMTP('smtp.gmail.com', 587)
        server.starttls()
        server.login(me, "degrace1")
        while indiceMail < len(lAd):
            msg['To'] = lAd[indiceMail]
            server.sendmail(me, lAd[indiceMail], msg.as_string())
            indiceMail = indiceMail + 1
        server.quit()
        lstre = {}
        lstre['resultat'] = 'ok'
        return ''

    #index.exposed = True

    #methodetest.exposed = True

    #sendMail.exposed = True
if __name__ == '__main__':
    print(os.path.dirname(os.getcwd()))
    conf = {
        '/': {
            'tools.sessions.on': True,
            'tools.staticdir.root': os.path.abspath(os.getcwd())
        },
        '/static': {
            'tools.staticdir.on': True,
            'tools.staticdir.dir':'./public'
        }
    }
    cherrypy.quickstart(MonSiteWeb(), '/', conf)

#cherrypy.quickstart(MonSiteWeb(), config ="server.conf")


from matplotlib import pyplot as plt

def plot_forest_management(gamma_values, iterations, time_array, rewards, title):
    plt.plot(gamma_values, rewards)
    plt.ylabel('Rewards')
    plt.xlabel('Discount')
    plt.title('{} - Reward vs Discount'.format(title))
    plt.grid()
    plt.show()

    plt.plot(gamma_values, iterations)
    plt.ylabel('Iterations to Converge')
    plt.xlabel('Discount')
    plt.title('{} - Convergence vs Discount'.format(title))
    plt.grid()
    plt.show()

    plt.plot(gamma_values, time_array)
    plt.title('{} - Execution Time vs Discount'.format(title))
    plt.xlabel('Discount')
    plt.ylabel('Time Taken (in seconds)')
    plt.grid()
    plt.show()


import os
import zipfile
from conftest import RESOURCES_DIR

def test_zip_file():
    with zipfile.ZipFile(os.path.join(RESOURCES_DIR, 'file_hello.zip')) as zip_file:
        zip_file.extract('file_hello.txt', path=RESOURCES_DIR)
        name_list = zip_file.namelist()
        print(name_list)
        text = zip_file.read('file_hello.txt')
        print(text)
        assert 'file_hello.txt' in name_list
        assert text == b'Hello'
        assert os.path.isfile(os.path.join(RESOURCES_DIR, 'file_hello.txt'))

        os.remove(os.path.join(RESOURCES_DIR, 'file_hello.txt'))


#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""Common fixtures and utils for io tests."""

import copy
import os

import pytest

from orion.core.evc import conflicts

@pytest.fixture()
def config_file():
    """Open config file with new config"""
    file_path = os.path.join(
        os.path.dirname(os.path.abspath(__file__)), "orion_config.yaml"
    )

    return open(file_path)

@pytest.fixture()
def old_config_file():
    """Open config file with original config from an experiment in db"""
    file_path = os.path.join(
        os.path.dirname(os.path.abspath(__file__)), "orion_old_config.yaml"
    )

    return open(file_path)

@pytest.fixture()
def incomplete_config_file():
    """Open config file with partial database configuration"""
    file_path = os.path.join(
        os.path.dirname(os.path.abspath(__file__)), "orion_incomplete_config.yaml"
    )

    return open(file_path)

@pytest.fixture
def parent_config():
    """Generate a new experiment configuration"""
    return dict(_id="test", name="test", metadata={"user": "corneauf"}, version=1)

@pytest.fixture
def child_config(parent_config):
    """Generate a new experiment configuration"""
    config = copy.deepcopy(parent_config)
    config["_id"] = "test2"
    config["refers"] = {"parent_id": "test"}
    config["version"] = 2

    return config

@pytest.fixture
def experiment_name_conflict(storage, parent_config, child_config):
    """Generate an experiment name conflict"""
    exps = storage.fetch_experiments({"name": "test"}) + storage.fetch_experiments(
        {"name": "test2"}
    )
    for exp in exps:
        storage.delete_experiment(uid=exp["_id"])
    storage.create_experiment(parent_config)
    storage.create_experiment(child_config)
    return conflicts.ExperimentNameConflict(parent_config, parent_config)


# DSC 510
# Week 11
# Programming Assignment Week 11
# Author: Reenie Christudass
# 05/23/2022

# Change#:1
# Change(s) Made: Cash register program
# Date of Change: 05/23/2022
# Author: Reenie Christudass
# Change Approved by: Michael Eller
# Date Moved to Production: 05/23/2022

import locale
from termcolor import colored

locale.setlocale(locale.LC_ALL, 'en_US')

class CashRegister:

    # constructor
    def __init__(self):
        self.total = 0.0
        self.itemCount = 0

    # setter
    def add_item(self, price):
        self.total = self.total + float(price)
        self.itemCount = self.itemCount + 1

    # getter
    def get_total(self):
        return self.total

    def get_count(self):
        return self.itemCount

def process_line(create_list):
    print("")
    print("---------------THANK YOU FOR USING CASH REGISTER---------------------------")
    print("")
    # Print all items purchased
    if create_list:
        for n in range(len(create_list)):
            text = colored(create_list[n], 'blue')
            print("Item Purchased " + str(n + 1) + ":" + '$ ' + text)
    else:
        print("")
        print("No items selected")

def main():
    print("WELCOME TO CASH REGISTER!!!!")
    register = CashRegister()
    create_list = []
    # variable declared to have the program in loop unless exited by the user
    item_loop = 0
    while item_loop >= 0:
        # Allow user to enter the price of the item
        if item_loop == 0:
            input_message = input("Enter the price of the item or enter exit to quit the program:")
        elif item_loop > 0:
            input_message = input("Enter the price of another item or enter exit to quit the program:")
        # read the file

        if input_message != 'exit':
            try:
                # program will convert the user input value to float
                input_message = float(input_message)
                create_list.append(input_message)
            except ValueError as e:
                # if the program couldn't convert the value to float , it will prompt the user to enter value in numeric
                print("Please enter the numeric value for item")
                continue

            register.add_item(input_message)
        # if the user input is equal to exit
        elif input_message == 'exit':
            break
        print()
        item_loop = item_loop + 1
    process_line(create_list)

    print("")
    message_output = ("Total Cost of the items purchased : {}".format(locale.currency(register.get_total())))
    print(colored(message_output, 'yellow'))
    message_output = ("Total Items Purchased: {}".format(colored(register.get_count()), 'blue'))
    print(colored(message_output, 'magenta'))
    print("")
    print("Thank you and have a great day")

# using special variable
if __name__ == "__main__":
    main()


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

from django.shortcuts import render
from django.http import HttpResponse, JsonResponse
from rest_framework.decorators import api_view
from django.shortcuts import get_object_or_404
from rest_framework import status
from rest_framework.views import APIView
from rest_framework.response import Response
from .models import *
from .serializers import *
# Create your views here.
def index(request):
	return HttpResponse('
The index page of App
')

class Wells(APIView):
	def get(self,request):
		welllist = Well.objects.all()
		serializer=WellSerializer(welllist, many=True)
		return Response(serializer.data)
	def post(self,request):
		pass

def maps(request):
    #template = loader.get_template('maps/maps.html')
    markers1={0:{'lat':-33.7772, 'long':151.1241}}
    return render(request, 'app/maps.html',{'points':markers1})

class MapList(APIView):
	def get(self, request):
		maplist = Farmpoints.objects.all()
		serializer=FarmpointSerializer(maplist, many=True)
		return Response(serializer.data)
	def post(self):
		pass

#class News(APIView):
#	def get(self,request):
	#	query= Farmpoints.objects.raw('SELECT * FROM Farm JOIN Farmer JOIN Farmpoints WHERE Farmpoints.Farm_id= 1')
#		return render(request, 'app/maps.html',{'farmdet':query})
#		query=Farmer.objects.raw('SELECT * FROM app_farmer JOIN app_farm ON app_farmer.id=1')
#		return render(request, 'app/abc.html',{'resu':query})

@api_view(['GET', 'PUT', 'DELETE'])
def MapDetail(request,pk, format=None):
	try:
		map_detail=Farmpoints.objects.get(pk=pk)
	except Farmpoints.DoesNotExist:
		return Response(status=status.HTTP_404_NOT_FOUND)

	if request.method=='GET':
		serializer=FarmpointSerializer(map_detail)
		return Response(serializer.data)

	elif request.method=='PUT':
		serializer=FarmpointSerializer(map_detail, data=request.data)
		if serializer.is_valid():
			serializer.save()
			return Response(serializer.data)
		return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

	elif request.method=='DELETE':
		map_detail.delete()
		return Response(status=status.HTTP_204_NO_CONTENT)

class FarmerList(APIView):
	def get(self,request):
		farmerlist = Farmer.objects.all()
		serializer=FarmerSerializer(farmerlist, many=True)
		return Response(serializer.data)
	def post(self,request):
		pass

@api_view(['GET', 'PUT', 'DELETE'])
def FarmerDetail(request,pk, format=None):
	try:
		f_detail=Farmer.objects.get(pk=pk)
	except Farmer.DoesNotExist:
		return Response(status=status.HTTP_404_NOT_FOUND)

	if request.method=='GET':
		serializer=FarmerSerializer(f_detail)
		return Response(serializer.data)

	elif request.method=='PUT':
		serializer=FarmerSerializer(f_detail, data=request.data)
		if serializer.is_valid():
			serializer.save()
			return Response(serializer.data)
		return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

	elif request.method=='DELETE':
		f_detail.delete()
		return Response(status=status.HTTP_204_NO_CONTENT)

class HouseHoldList(APIView):
	def get(self, request):
		hhlist = HouseHold.objects.all()
		serializer=HouseHoldSerializer(hhlist, many=True)
		return JsonResponse(serializer.data, safe=False)
	def post(self):
		pass
@api_view(['GET', 'PUT', 'DELETE'])
def HouseHoldDetail(request,pk, format=None):
	try:
		hh_detail=HouseHold.objects.get(pk=pk)
	except HouseHold.DoesNotExist:
		return Response(status=status.HTTP_404_NOT_FOUND)

	if request.method=='GET':
		serializer=HouseHoldSerializer(hh_detail)
		return Response(serializer.data)

	elif request.method=='PUT':
		serializer=HouseHoldSerializer(hh_detail, data=request.data)
		if serializer.is_valid():
			serializer.save()
			return Response(serializer.data)
		return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

	elif request.method=='DELETE':
		hh_detail.delete()
		return Response(status=status.HTTP_204_NO_CONTENT)

class MemberList(APIView):
	def get(self, request):
		memberlist = Member.objects.all()
		serializer=MemberSerializer(memberlist, many=True)
		return Response(serializer.data)
	def post(self):
		pass
@api_view(['GET', 'PUT', 'DELETE'])
def MemberDetail(request,pk, format=None):
	try:
		m_detail=Member.objects.get(pk=pk)
	except Member.DoesNotExist:
		return Response(status=status.HTTP_404_NOT_FOUND)

	if request.method=='GET':
		serializer=MemberSerializer(m_detail)
		return Response(serializer.data)

	elif request.method=='PUT':
		serializer=MemberSerializer(m_detail, data=request.data)
		if serializer.is_valid():
			serializer.save()
			return Response(serializer.data)
		return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

	elif request.method=='DELETE':
		m_detail.delete()
		return Response(status=status.HTTP_204_NO_CONTENT)

class FarmList(APIView):
        def get(self, request):
                flist = Farm.objects.all()
                serializer=FarmSerializer(flist, many=True)
                return Response(serializer.data)
        def post(self):
                pass


import os

from flask import Blueprint, jsonify

from ..decorators import required_login
from ..utilities import create_jwt

jwt_rest_bp = Blueprint('jwt_rest_bp', __name__)

@jwt_rest_bp.route('/api/v1/user', methods=['GET'])
@required_login
def jwt(user):
    username = user.username
    email = user.email
    token = create_jwt(username, email)
    return jsonify(token=token.decode('utf-8')), 200

@jwt_rest_bp.route('/api/v1/service_get_pub', methods=['GET'])
def get_pub():
    pub = os.environ.get('public_key')
    return jsonify(pub=pub), 200


""" Module for tables of the Auto Typing paper
"""

# Imports
from __future__ import print_function, absolute_import, division, unicode_literals

import numpy as np
import glob, os, sys
import warnings
import pdb

from pkg_resources import resource_filename

from astropy import units as u
from astropy.table import Table
from astropy.coordinates import SkyCoord, match_coordinates_sky
from astropy.time import Time

from linetools import utils as ltu

from spit import labels as spit_lbl

ddict = {'JAN': 1, 'FEB': 2, 'MAR': 3, 'APR': 4, 'MAY': 5,
    'JUN': 6, 'JUNE': 6, 'JUL': 7, 'JULY': 7, 'AUG': 8,
    'SEP': 9, 'SEPT': 9, 'OCT': 10, 'NOV': 11, 'DEC': 12}

# Local
#sys.path.append(os.path.abspath("../Analysis/py"))
#sys.path.append(os.path.abspath("../Vetting/py"))
#from vette_dr7 import load_ml_dr7

def mktab_images(outfil='tab_images.tex', sub=False):

    # Path
    path = os.getenv('SPIT_DATA')+'/Kast/FITS/'
    if sub:
        outfil = 'tab_images_sub.tex'

    # Scan image sets
    types, dates, frames, pis, flags, sets = [], [], [], [], [], []
    ntest, ntrain, nvalid = 0, 0, 0
    for iset in ['test', 'train', 'validation']:
        for itype in ['arc', 'bias', 'flat', 'science', 'standard']:
            files = glob.glob(path+'{:s}/{:s}/*fits.gz'.format(iset,itype))
            nimg = len(files)
            print("There are {:d} images of type {:s} in set {:s}".format(nimg, itype, iset))
            #
            types += [itype]*nimg
            sets += [iset]*nimg
            if iset == 'test':
                ntest += nimg
            elif iset == 'train':
                ntrain += nimg
            elif iset == 'validation':
                nvalid += nimg
            # Loop on the images
            for ipath in files:
                # Parse
                ifile = os.path.basename(ipath)
                if ifile[0] == '2':
                    pis += ['Prochaska']
                    # Find date
                    year = ifile[0:4]
                    try:
                        month = ddict[ifile[4:7].upper()]
                    except KeyError:
                        pdb.set_trace()
                    day = ifile[7:9]
                    dates.append('{:s}-{:d}-{:s}'.format(year,month,day))
                    # Frame
                    i1 = ifile.find('.fits')
                    frames.append(ifile[10:i1])
                else:
                    pis += ['Hsyu']
                    # Find date
                    i0 = ifile.find('_20')
                    year = ifile[i0+1:i0+5]
                    try:
                        month = ddict[ifile[0:4].upper()]
                    except:
                        month = ddict[ifile[0:3].upper()]
                        day = ifile[3:i0]
                    else:
                        day = ifile[4:i0]
                    dates.append('{:s}-{:d}-{:s}'.format(year,month,day))
                    # Frame
                    i1 = ifile.find('.fits')
                    frames.append(ifile[i0+6:i1])

    # Summarize
    print('-----------------------------------------')
    print('There are a total of {:d} training images'.format(ntrain))
    print('There are a total of {:d} test images'.format(ntest))
    print('There are a total of {:d} validation images'.format(nvalid))
    print('There are a total of {:d} images!'.format(nvalid+ntrain+ntest))

    # Build the table
    tbl = Table()
    tbl['Type'] = types
    tbl['Date'] = dates
    tbl['Frame'] = frames
    tbl['PI'] = pis
    tbl['set'] = sets
    # Sort
    tbl.sort(['Type', 'Date', 'Frame'])
    # Time
    t = Time(tbl['Date'], out_subfmt='date')

    # Make the LateX Table
    # Open
    tbfil = open(outfil, 'w')

    # tbfil.write('\\clearpage\n')
    tbfil.write('\\begin{deluxetable}{lccccc}\n')
    # tbfil.write('\\rotate\n')
    tbfil.write('\\tablewidth{0pc}\n')
    tbfil.write('\\tablecaption{Training Set\\label{tab:images}}\n')
    tbfil.write('\\tabletypesize{\\small}\n')
    tbfil.write('\\tablehead{\\colhead{Type?} & \\colhead{Date} \n')
    tbfil.write('& \\colhead{Frame} \n')
    tbfil.write('& \\colhead{Usage} \n')
    tbfil.write('} \n')

    tbfil.write('\\startdata \n')

    for ii,row in enumerate(tbl):
        if (ii > 15) & sub:
            break

        # Line
        iline = '{:s} & {:s} & {:s} & {:s}'.format(row['Type'], t[ii].value,
                                                   row['Frame'], row['set'])

        # End line
        tbfil.write(iline)
        tbfil.write('\\\\ \n')

    # End Table
    # End
    tbfil.write('\\enddata \n')
    #tbfil.write('\\tablenotetext{a}{Star/galaxy classifier from SExtractor with S/G=1 a star-like object.  Ignored for $\\theta < \\theta_{\\rm min}$.}\n')
    # tbfil.write('\\tablecomments{Units for $C_0$ and $C_1$ are erg/s/cm$^2$/\\AA\ and erg/s/cm$^2$/\\AA$^2$ respecitvely.}\n')
    # End
    tbfil.write('\\end{deluxetable} \n')
    #tbfil.write('\\hline \n')
    #tbfil.write('\\end{tabular} \n')
    #tbfil.write('\\end{minipage} \n')
    #tbfil.write('{$^a$}Restricted to systems with $\mzabs < \mzem$.\\\\ \n')
    #tbfil.write('{$^b$}Quasar is reported to exhibit BAL features by \cite{shen11} (1=True).  We caution that additional BAL features exist in the purported non-BAL quasars.\\\\ \n')
    #tbfil.write('{$^c$}DLA is new (0) or is also reported by N09 (1), PW09 (2), or both (3).\\\\ \n')
    #tbfil.write('\\end{table*} \n')

    #tbfil.write('\\enddata \n')
    #tbfil.write('\\tablenotetext{a}{Flag describing the continuum method applied: 0=Analysis based only on Lyman series lines; 1=Linear fit; 2=Constant fit; 3=Continuum imposed by hand.}\n')
    #tbfil.write('\\tablecomments{Units for $C_0$ and $C_1$ are erg/s/cm$^2$/\\AA\ and erg/s/cm$^2$/\\AA$^2$ respecitvely.}\n')
    # End
    #tbfil.write('\\end{deluxetable*} \n')

    tbfil.close()
    print('Wrote {:s}'.format(outfil))

#### ########################## #########################
def main(flg_tab):

    if flg_tab == 'all':
        flg_tab = np.sum( np.array( [2**ii for ii in range(5)] ))
    else:
        flg_tab = int(flg_tab)

    # DR7 Table
    if flg_tab & (2**0):
        mktab_images(sub=True)# outfil='tab_dr7_dlas_sub.tex', sub=True)
        mktab_images()# outfil='tab_dr7_dlas_sub.tex', sub=True)

# Command line execution
if __name__ == '__main__':

    if len(sys.argv) == 1:
        flg_tab = 0
        flg_tab += 2**0   # Image table
        #flg_tab += 2**1   # DR12
    else:
        flg_tab = sys.argv[1]

    main(flg_tab)


#!/usr/bin/python3
import arcade

screen_width = 600
screen_height = 600

arcade.open_window(screen_width, screen_width, "drawing example")
arcade.set_background_color(arcade.color.WHITE)
arcade.start_render()

# draw the face
x = 300
y = 300
radius = 200
arcade.draw_circle_filled(x, y, radius, arcade.color.YELLOW)
# draw the right eye
x = 370
y = 350
radius = 20
arcade.draw_circle_filled(x, y, radius, arcade.color.BLACK)
# draw the LEFT EYE
x = 230
y = 350
radius = 20
arcade.draw_circle_filled(x, y, radius, arcade.color.BLACK)
# draw the smile
x = 300
y = 280
width = 120
height = 100
start_angle = 190
end_angle = 350
radius = 200
arcade.draw_arc_outline(x, y, width, height, arcade.color.BLACK, start_angle, end_angle, 10)
arcade.finish_render()
arcade.run()


import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt

student_data = pd.read_csv(
    "data/HSLS_2016_v1_0_CSV_Datasets/hsls_16_student_v1_0.csv", na_values=[-9, -8, -5, -7, -4, -3])
student_data.head()

# -9 = No Unit Response
# -8 = Missing
# -5 = Supressed
# -7 = Skipped
# -4 = Question not adminstered
# -3 = Carry through missing
# Sort by the first gathered predictor variables

# Getting columns that start with certain keywords
filter_col = [col for col in student_data if col.startswith('S1') or col.startswith('X1') or col.startswith('A1') or col.startswith(
    'C1')or col.startswith('STU_ID')]
X = student_data[filter_col]

updated_predictor_col_count = X.isna().sum()
df = updated_predictor_col_count / len(X)
df.to_csv("figures/missingness_imputation_missingness_per_predictor.csv")

# Only take columns with less than 15% of the data missing
X = X[X.columns[X.isnull().mean() < 0.15]]

# Median Imputation
updated_predictor_col = X.fillna(
    X.median())
updated_predictor_col.head()

fig, ax = plt.subplots(figsize=(10, 6.7))
sns.set_style('whitegrid')
sns.countplot(x='X1SEX', data=X, palette="mako")
sns.countplot(x='X1RACE', data=X, palette="mako")

X.columns

# Export to CSV file
updated_predictor_col.to_csv("data/processed/baseline_features.csv", index=False)


import argparse
import importlib
from collections import namedtuple
from typing import Dict

CommandInfo = namedtuple("CommandInfo", "module_path, class_name")

commands_dict: Dict[str, CommandInfo] = {
    "download": CommandInfo("gtd.cli.download", "DownloadCommand"),
    "export": CommandInfo("gtd.cli.export", "ExportCommand"),
    "ts": CommandInfo("gtd.cli.ts", "TsCommand"),
}

class Command:
    def __init__(self, name: str) -> None:
        self.name = name

    def run(self, args: argparse.Namespace) -> int:
        raise NotImplementedError

def create_command(name: str) -> Command:
    """
    Create an instance of the Command class with the given name.
    """
    module_path, class_name = commands_dict[name]
    module = importlib.import_module(module_path)
    command_class = getattr(module, class_name)
    command: Command = command_class(name=name)

    return command


#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Author: Yijia Zheng
# @email : yj.zheng@siat.ac.cn
# @Time  : 2019/11/29 11:20:37

import rdkit
import rdkit.Chem as Chem
import copy
import sys
import argparse
from multiprocessing import Pool

from util.chemutils import get_clique_mol, tree_decomp, get_mol, get_smiles, set_atommap, enum_assemble, decode_stereo
from branch_jtnn.mol_tree import MolTree

lg = rdkit.RDLogger.logger()
lg.setLevel(rdkit.RDLogger.CRITICAL)

parser = argparse.ArgumentParser()
parser.add_argument('--mol_file', required=True)
parser.add_argument('--save_vocab_file', required=True)
parser.add_argument('--ncpu', type=int, default=8)
args = parser.parse_args()

cset = set()
with open(args.mol_file, 'r') as f:
    lines = f.readlines()
lines = [line[:-1] for line in lines]

def get_tree(smiles):
    return MolTree(smiles, 0)

pool = Pool(args.ncpu)
trees = pool.map(get_tree, lines)

for smiles in lines:
    mol = Chem.MolFromSmiles(smiles)
    for i in range(mol.GetNumAtoms()):
        cmol = get_clique_mol(mol, [i])
        cset.add(get_smiles(cmol))
for tree in trees:
    for c in tree.nodes:
        cset.add(c.smiles)

result = '\n'.join(sorted(cset))
with open(args.save_vocab_file, 'w') as f:
    f.write(result)

print('done', flush=True)


from flask import Flask, render_template, request
from flask_sqlalchemy import SQLAlchemy
from sqlalchemy import inspect

app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///portfolio.db'
db = SQLAlchemy(app)

class Portfolio(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    title = db.Column(db.String(100))
    link = db.Column(db.String(100))

    def __init__(self, title, link):
        self.title = title
        self.link = link

@app.route('/')
def index():
    portfolios = Portfolio.query.all()
    return render_template('index.html', portfolios=portfolios)

@app.route('/drop', methods=['GET', 'POST'])
def drop_db():
    if request.method == 'POST':
        db.drop_all()
        db.session.commit()
        dbInit()

    portfolios = Portfolio.query.all()
    return render_template('index.html', portfolios=portfolios)

@app.route('/add', methods=['GET', 'POST'])
def add_portfolio():
    if request.method == 'POST':
        title = request.form['title']
        link = request.form['link']
        portfolio = Portfolio(title=title, link=link)
        db.session.add(portfolio)
        db.session.commit()
    return render_template('add.html')

def dbInit():
    with app.app_context():
        inspector = inspect(db.engine)
        if not inspector.has_table('portfolio'):
            db.create_all()
            print('Таблица "portfolio" создана')
        else:
            print('Таблица "portfolio" уже существует')

if __name__ == '__main__':
    dbInit()
    app.run(debug=True)


"""Utilities for parsing isoformat strings into datetime types.
"""

from datetime import datetime, date, time

def datetimefromisoformat(s):
    """Parse an isoformat string into a datetime.datetime object.
    """
    date, time = s.split()
    year, month, day = _parsedate(date)
    hour, minute, second, microsecond = _parsetime(time)
    return datetime(year, month, day, hour, minute, second, microsecond)

def datefromisoformat(s):
    """Parse an isoformat date string into a datetime.date object.
    """
    year, month, day = _parsedate(s)
    return date(year, month, day)

def timefromisoformat(s):
    """Parse an isoformat time string into a datetime.time object.
    """
    hour, minute, second, microsecond = _parsetime(s)
    return time(hour, minute, second, microsecond)

def _parsedate(s):
    """Parse an isoformat date string into a (year, month, day) tuple.
    """
    return [int(i) for i in s.split('-')]

def _parsetime(s):
    """Parse an isoformat time string into a (hour, minute, second,
    microsecond) tuple.
    """

    # The microseond is optional
    if '.' in s:
        time, microsecond = s.split('.')
        microsecond = int(microsecond)
    else:
        time, microsecond = s, 0

    hour, minute, second = [int(i) for i in time.split(':')]

    return hour, minute, second, microsecond

if __name__ == '__main__':

    import unittest

    now = datetime.now()
    today = date.today()
    midnight = time()

    class Tests(unittest.TestCase):

        datetimeTests = [
            [str(now), str(now)],
            [str(now.replace(microsecond=0)), str(now.replace(microsecond=0))],
            [str(now.replace(second=0, microsecond=0)), str(now.replace(second=0, microsecond=0))],
            [str(now.replace(second=0)), str(now.replace(second=0))],
            ]

        dateTests = [
            [str(today), str(today)],
            ]

        timeTests = [
            [str(time(0,0,0)),'00:00:00'],
            [str(time(1,2,3)),'01:02:03'],
            [str(time(1,2,3,4)),'01:02:03.000004'],
            ]

        def test_datetime(self):
            for test,expected in self.datetimeTests:
                result = str(datetimefromisoformat(test))
                self.assertEqual(expected, result)

        def test_date(self):
            for test,expected in self.dateTests:
                result = str(datefromisoformat(test))
                self.assertEqual(expected, result)

        def test_time(self):
            for test,expected in self.timeTests:
                result = str(timefromisoformat(test))
                self.assertEqual(expected, result)

    unittest.main()


import json
import urllib

from django.conf import settings

def evaluate_recaptcha(request, errors):
    # Google Recaptcha validation
    recaptcha_response = request.POST.get('g-recaptcha-response')
    url = 'https://www.google.com/recaptcha/api/siteverify'
    values = {
        'secret': settings.GOOGLE_RECAPTCHA_PRIVATE_KEY,
        'response': recaptcha_response
    }
    data = urllib.parse.urlencode(values).encode()
    req = urllib.request.Request(url, data=data)
    response = urllib.request.urlopen(req)
    result = json.loads(response.read().decode())
    if not result['success']:
        errors.append('Site access denied, Google reCaptcha authentication failed')


from aiogram import types, Dispatcher
from aiogram.dispatcher import FSMContext
from aiogram.dispatcher.filters import Text
from aiogram.dispatcher.filters.state import State, StatesGroup
from keyboards import admin_batton
from create_bot import bot, dp
from data_base import sqlite_db
from aiogram.types import InlineKeyboardButton, InlineKeyboardMarkup

ID = None

class FSMAdmin(StatesGroup):
    photo = State()
    name = State()
    description = State()
    price = State()

# @dp.message_handler(commands=['moderator'], is_chat_admin=True)
async def make_changes_command(message: types.Message):
    global ID
    ID = message.from_user.id
    await bot.send_message(message.from_user.id, "Что нужно хозяин?", reply_markup=admin_batton.button_case_admin)

# @dp.message_handler(commands='Загрузить', state=None)
async def cm_start(message: types.Message):
    if message.from_user.id == ID:
        await FSMAdmin.photo.set()
        await message.reply("Загрузи фото")

# @dp.message_handler(state="*", commands='отмена')
# @dp.message_handler(Text(equals='отмена', ignore_case=True), state="*")
async def cansel_handler(message: types.Message, state: FSMContext):
    if message.from_user.id == ID:
        current_state = await state.get_state()
        if current_state is None:
            return
        await state.finish()
        await message.reply('OK')

# @dp.message_handler(content_types=['photo'], state=FSMAdmin.photo)
async def load_photo(message: types.Message, state: FSMContext):
    if message.from_user.id == ID:
        async with state.proxy() as data:
            data['photo'] = message.photo[0].file_id

        await FSMAdmin.next()
        await message.reply('Теперь название')

# @dp.message_handler(state=FSMAdmin.name)
async def load_name(message: types.Message, state: FSMContext):
    if message.from_user.id == ID:
        async with state.proxy() as data:
            data['name'] = message.text

        await FSMAdmin.next()
        await message.reply('Введи описание')

# @dp.message_handler(state=FSMAdmin.description)
async def load_description(message: types.Message, state: FSMContext):
    if message.from_user.id == ID:
        async with state.proxy() as data:
            data['description'] = message.text

        await FSMAdmin.next()
        await message.reply('Какова цена?')

# @dp.message_handler(state=FSMAdmin.price)
async def load_price(message: types.Message, state: FSMContext):
    if message.from_user.id == ID:
        async with state.proxy() as data:
            data['price'] = float(message.text)

        await sqlite_db.sql_add_command(state)
        await state.finish()

@dp.callback_query_handler()
async def del_callback_run(callback_query: types.CallbackQuery):
    await sqlite_db.delete_command(callback_query.data.replace('del ', ''))
    await callback_query.answer(text=f'{callback_query.data.replace("del ", "")} удалена.', show_alert=True)

@dp.message_handler(commands='Удалить')
async def delete_item(message: types.Message):
    if message.from_user.id == ID:
        read = await sqlite_db.sql_read2()
        for ret in read:
            await bot.send_photo(message.from_user.id, ret[0], f'{ret[1]}\nОписание: {ret[2]}\nЦена {ret[-1]}')
            await bot.send_message(message.from_user.id, text='^^^', reply_markup=InlineKeyboardMarkup(). \
                                   add(InlineKeyboardButton(f'Удалить {ret[1]}', callback_data=f'del {ret[1]}')))

def register_handler_admin(dp: Dispatcher):
    dp.register_message_handler(cm_start, commands=['Загрузить'], state=None)
    dp.register_message_handler(cansel_handler, state="*", commands='отмена')
    dp.register_message_handler(cansel_handler, Text(equals='отмена', ignore_case=True), state="*")
    dp.register_message_handler(load_photo, content_types=['photo'], state=FSMAdmin.photo)
    dp.register_message_handler(load_name, state=FSMAdmin.name)
    dp.register_message_handler(load_description, state=FSMAdmin.description)
    dp.register_message_handler(load_price, state=FSMAdmin.price)
    dp.register_message_handler(make_changes_command, commands=['moderator'], is_chat_admin=True)
    # dp.register_message_handler(del_callback_run, lambda x: x.data and x.data.startwith('del '))
    # dp.register_message_handler(delete_item, commands='Удалить')


import typing as t
import os.path
import logging
import json

from yaml.error import Mark
from typing_extensions import TypedDict, Protocol, Literal

from schemalint.entity import ErrorEvent, Lookup, Context
from schemalint.errors import (
    ParseError,
    LintError,
    ResolutionError,
    ValidationError,
    MessageError,
)

logger = logging.getLogger(__name__)

StatusType = Literal["INFO", "WARNING", "ERROR"]

class PositionDict(TypedDict):
    line: int
    character: int

class OutputDict(TypedDict):
    status: str
    errortype: str
    filename: str

    start: PositionDict
    end: PositionDict

    message: str
    where: t.List[str]

class Layout(Protocol):
    def layout(self, d: OutputDict) -> str:
        ...

class LTSVLayout(Layout):
    def layout(self, d: OutputDict) -> str:
        d["start"] = f"{d['start']['line']}@{d['start']['character']}"
        d["end"] = f"{d['end']['line']}@{d['end']['character']}"
        return "\t".join(f"{k}:{v}" for k, v in d.items())

class JSONLayout(Layout):
    def layout(self, d: OutputDict) -> str:
        d["where"] = str(d["where"])
        return json.dumps(d, ensure_ascii=False)

class Detector:
    lookup: Lookup

    def __init__(self, filename: str, *, lookup: Lookup):
        self.filename = filename  # root file
        self.lookup = lookup

    def has_error_point(self, err: LintError):
        return getattr(err, "problem_mark", None) is not None

    def detect_status(self, filename):
        if self.filename == filename:
            return "ERROR"
        else:
            return "WARNING"

    def detect_loadning_start_point(self, err: LintError) -> (Mark, Mark):
        if err.data is None:
            return self.detect_error_point(err)
        map_node = self.lookup.lookup_node(err.data)
        knode, vnode = self.lookup_kvpair(map_node, err.path[-1])
        return knode.start_mark, vnode.end_mark

    def detect_error_point(self, err: LintError) -> Mark:
        mark = getattr(err.inner, "context_mark")
        import copy

        if mark is None:
            mark = getattr(err.inner, "problem_mark")
            mark.line -= 1  # xxx

        start_mark = copy.deepcopy(mark)
        start_mark.column = 0
        end_mark = copy.deepcopy(mark)
        end_mark.column = -1
        return (start_mark, end_mark)

    def lookup_kvpair(self, node, k):  # todo: rename
        for knode, vnode in node.value:
            if knode.value == k:
                return knode, vnode

class Formatter:
    detector: Detector
    layout: Layout

    def __init__(
        self, filename: str, *, detector: Detector, layout: t.Optional[Layout] = None
    ):
        self.filename = filename
        self.detector = detector
        self.layout = layout or LTSVLayout()

    def format(self, ev: ErrorEvent) -> str:
        err = ev.error
        if isinstance(err, ParseError):
            return self.format_parse_error(err)
        elif isinstance(err, ResolutionError):
            return self.format_resolution_error(err)
        elif isinstance(err, ValidationError):
            return self.format_validation_error(err)
        elif isinstance(err, MessageError):
            return self.format_message_error(err, context=ev.context)
        else:
            raise err

    def format_parse_error(self, err: ParseError) -> str:
        status = self.detector.detect_status(err.history[-1])
        if hasattr(err.inner, "problem"):
            message = f"{err.inner.problem} ({err.inner.context})"
        else:
            message = repr(err.inner)

        start_mark, end_mark = self.detector.detect_loadning_start_point(err)
        filename = os.path.relpath(start_mark.name, start=".")

        where = [os.path.relpath(name) for name in err.history]
        where[0] = f"{where[0]}:{start_mark.line+1}"
        if self.detector.has_error_point(err):
            where[-1] = f"{where[-1]}:{err.inner.problem_mark.line+1}"

        return self.layout.layout(
            OutputDict(
                status=status,
                errortype=err.__class__.__name__,
                filename=filename,
                start=PositionDict(
                    line=start_mark.line + 1, character=start_mark.column
                ),
                end=PositionDict(line=end_mark.line + 1, character=end_mark.column),
                message=message,
                where=where,
            )
        )

    def format_resolution_error(self, err: ResolutionError) -> str:
        start_mark, end_mark = self.detector.detect_loadning_start_point(err)
        filename = os.path.relpath(start_mark.name, start=".")
        status = self.detector.detect_status(err.history[-1])
        message = repr(err.inner)

        where = [os.path.relpath(name) for name in err.history]
        where[0] = f"{where[0]}:{start_mark.line+1}"
        if self.detector.has_error_point(err):
            where[-1] = f"{where[-1]}:{err.inner.problem_mark.line+1}"

        return self.layout.layout(
            OutputDict(
                status=status,
                errortype=err.__class__.__name__,
                filename=filename,
                start=PositionDict(
                    line=start_mark.line + 1, character=start_mark.column
                ),
                end=PositionDict(line=end_mark.line + 1, character=end_mark.column),
                message=message,
                where=where,
            )
        )

    def format_validation_error(self, err: ValidationError) -> str:
        status = "ERROR"
        message = f"{err.message} (validator={err.validator})"
        node = self.detector.lookup.lookup_node(err.instance)  # xxx

        start_mark, end_mark = node.start_mark, node.end_mark

        filename = os.path.relpath(start_mark.name, start=".")
        where = [os.path.relpath(filename)]
        where[0] = f"{where[0]}:{start_mark.line+1}"

        return self.layout.layout(
            OutputDict(
                status=status,
                errortype=err.__class__.__name__,
                filename=filename,
                start=PositionDict(
                    line=start_mark.line + 1, character=start_mark.column
                ),
                end=PositionDict(line=end_mark.line + 1, character=end_mark.column),
                message=message,
                where=where,
            )
        )

    def format_message_error(
        self,
        err: MessageError,
        *,
        context: t.Optional[Context],
        status: StatusType = "INFO",
    ) -> str:
        message = err.args[0]
        filename = os.path.relpath(context.filename if context else self.filename)
        where = [filename]
        return self.layout.layout(
            OutputDict(
                status=status,
                errortype=err.__class__.__name__,
                filename=filename,
                start=PositionDict(line=1, character=1),
                end=PositionDict(line=1, character=-1),
                message=message,
                where=where,
            )
        )

OutputType = Literal["ltsv", "json"]

def get_formatter(
    filename: str, *, lookup: Lookup, output_type: OutputType
) -> Formatter:
    detector = Detector(filename, lookup=lookup)
    layout = get_layout(output_type)
    return Formatter(filename, detector=detector, layout=layout)

def get_layout(output_type: OutputType) -> Layout:
    if output_type == "json":
        return JSONLayout()
    else:
        return LTSVLayout()


#!/local/data/atorus1/dora/Compilers/epd-7.3-1-rh5-x86_64(1)/bin/python

##!/Library/Frameworks/Python.framework/Versions/Current/bin/python
##!/Users/dora/Library/Enthought/Canopy_32bit/User/bin/python

import scipy
from  numpy import ndarray, zeros, array, size, sqrt, meshgrid, flipud, floor, where, amin, argmin,int
import numpy as nm
from mpl_toolkits.mplot3d import Axes3D
from pylab import *
import pprint
import os
import glob
import time
import os
import time
import cPickle as pickle
from matplotlib import colors
import socket
from mpl_toolkits.axes_grid1 import AxesGrid, ImageGrid

from scipy import trapz
from matplotlib import pyplot as plt
from physics1 import *

import physics1 as ph

phToSHow = 1
import AthenaModel as ath

def plotFormat(ax1,ax2=None,ax3=None,im=None):
#     from mpl_toolkits.axes_grid1 import make_axes_locatable

    fontsize_bar=14
    fz = 16

#     ax1.set_ylabel('$\tau N(\tau(\theta)>1)$', fontsize = 22)
#     ax1.set_title('Obscuring models', fontsize = 19)
#
#     ax1.set_ylabel('log (Number of obscuring models)', fontsize = 19)
#     for ylabel in ax.get_yticklabels():
#         ylabel.set_fontsize(fontsize_x)
#
#     ax1.set_xlabel('Inclination angle', fontsize=fz)
    ax[0].set_ylabel('$\log(N_{col})$', fontsize=fz)
#     for xlabel in ax1.get_xticklabels():
#         xlabel.set_fontsize(fontsize_x)
#
#     ax1.set_title('Column density $(cm^{-2})$', fontsize =fz)

    fig.subplots_adjust(wspace=0.2)
#     plt.setp([a.get_yticklabels() for a in fig.axes[1:] ], visible=False)

#     ax1.set_ylabel('$log (N_{col})$', fontsize = 19)

#     for ylabel in ax.get_yticklabels():
#         ylabel.set_fontsize(fontsize_x)
#
#     ax2.set_xlabel('Inclination angle', fontsize = 19)
#     for xlabel in ax.get_xticklabels():
#         xlabel.set_fontsize(fontsize_x)

#     divider = make_axes_locatable(ax)
#     cax = divider.append_axes("right", size="5%", pad=0.05)
#
#     if (im):
#         cb =plt.colorbar(im, cax=cax)
#         for t in cb.ax.get_yticklabels():
#             t.set_fontsize(fontsize_bar)

def cellTrace(dat, th, Xsrc_x_z, phToSHow):
        tiny = 1e-18
        i_s = dat.i_s
        ie = dat.ie
        js = dat.js
        je = dat.je

        zout = dat.x[je] / nm.tan(th) + Xsrc_x_z[1]

        ic = abs(dat.z - zout).argmin()
        jc = je

#         xcur = nm.array([ dat.x[jc], dat.z[ic] ])

        xcur = nm.array([ dat.x[dat.je], zout ])

        xsrc0= Xsrc_x_z

        a = xcur - xsrc0

        norm = (xcur - xsrc0)/nm.dot( xcur - xsrc0, xcur - xsrc0)
        #crossing x_{js} plane
        xz1 =nm.array([ dat.x[js+1], norm[1]/max(norm[0], tiny)*(dat.x[js+1] - xsrc0[0]) + xsrc0[1] ])

#         i =  minloc((dat. z -xz1[1]) **2)
        i = abs(dat.z - xz1[1]).argmin()
        if i>=dat.ie or i==dat.i_s: return(0.,0.)

        j = dat.js
        xz0 = nm.array([ dat.x[j], dat.z[i] ])

        stp = 0
        dstot = 0.
        tau = 0.
        dtau = 0.
        dcol_dns = 0.
        col_dns = 0.

        dl = 0.

        while True:

                xz1 =nm.array([ dat.x[j+1], #crossing x_{j+1} plane
                    norm[1]/max(norm[0], tiny)*(dat.x[j+1] - xsrc0[0]) + xsrc0[1] ])

                Sx = sqrt(nm.dot(xz1 - xz0, xz1-xz0))

                itm = i+  nm.int( nm.sign( norm[1] ) )# crossing z_{i+-1} plane

                if (norm[1] != 0.):
                    xz2 =nm.array([ norm[0]/norm[1] *(dat.z[itm] - xsrc0[1]) + xsrc0[0],
                                dat.z[itm] ])
                else:
                    xz2 =nm.array([ norm[0]/nm.max(norm[1],tiny)*(dat.z[itm] - xsrc0[1]) + xsrc0[0],
                                dat.z[itm] ])

                Sz = sqrt(nm.dot(xz2 - xz0, xz2-xz0))

                if (Sz>Sx):
                    dl = Sx  #right
                    xz0 = xz1
                    j += 1
                else:
                    dl = Sz #up or down
                    xz0 = xz2
                    i = itm

                dstot += dl
                stp += 1
                opac = ph.KPE

#                 print "here", dat.Rsc, dat.Dsc,  dat.ro[i,phToSHow, j],  opac, dl,i,j

                dtau = dat.Rsc*dat.Dsc* dat.ro[i,phToSHow,j]* opac * dl

                dcol_dns = nm.fabs( dat.ro[i, phToSHow, j]*dat.n0* dat.Rsc*dl)

                tau += dtau
                col_dns += dcol_dns
#                 print i, j, stp

                if  ( i <= dat.i_s or i >= dat.ie or j <= dat.js or j >= dat.je-1):
                    break
                if  i==ic and j ==jc :
                    break

        return(tau, col_dns)

def transmisionFunctionsAngleGrid(dat, printResults=False):
    Nth = 100

    Xsrc = array([0, 0])

#     res = dat.x[dat.js]/dat.z[dat.ie]
#     thmax = nm.pi / 2
    thmin = nm.arctan2(dat.x[dat.js], dat.z[dat.ie]-Xsrc[0] )

    thmax =nm.arctan2(dat.x[dat.js], dat.z[dat.i_s]-Xsrc[0])

    angle = linspace(thmin, thmax, Nth)

#     print angle, range(1,Nth)
#     time.sleep(3)

    tauTheta = zeros(Nth)
    colDens = zeros(Nth)

    for k,th in zip(range(1,Nth), angle):
#         th = nm.pi/2
#         print k,th
#         time.sleep(3)
        tauTheta[k], colDens[k] = cellTrace(dat, th, Xsrc_x_z = [Xsrc[1], Xsrc[0]], phToSHow =phToSHow)
#         print("tau=", tauTheta[k], "colDens=", colDens[k], th, Nth)

#         exit(); time.sleep(3)

    if printResults:
        fig  = plt.figure()
        ax = fig.add_subplot(111)
        funcToPlot =  log10(colDens)
        funcToPlot =  tauTheta
        ax.plot(angle*180./nm.pi, funcToPlot)
        show()

    return(tauTheta,colDens, angle)

def iteratorOverDataDirectoriesOverHDfFiles(basePath, dataDir,
                                            simParamFileDir, locdirList2, funcToCalculate, fileToSavePrefix):

    mod={}
    maxNumFile = 500

    dat =ath.athDataModel()

    for dirName, i in zip(locdirList2, range(size(locdirList2))):

        mod.update({ locdirList2[i]:{'ang':[], 'tau':[], 'cdens':[] }})
        simTime = []

        nFile = 0.
        dat.loadSimulationParam(simParamFileDir + 'athinput.torus9_hydro_2D', print_res=True)

        scale = 1.
        dat.n0 /= scale
        dat.Dsc/=scale

        filelist = glob.glob(os.path.join(dataDir, 'mhdXwind*.bin') )

        for fileInDir in sorted(filelist):
               try:
                  dat.loadDataFromBinFiles(fileInDir, dat, printDetail=False)
                  print("file to open:", fileInDir)
               except:
                 print("skip file:", fileInDir)
                 break

             #  torMass = dat.torusMass(dat)

               # print(torMass/MSOL)
               # exit()

               tau, cdens, ang  = funcToCalculate(dat, printResults=False)

#                 print tau, cdens, ang

               mod[locdirList2[i]]['tau'].append(tau.tolist())
               mod[locdirList2[i]]['cdens'].append(cdens.tolist())

               if not mod[ locdirList2[i]]['ang']  :
                   mod[locdirList2[i]]['ang'].append(ang.tolist())

               simTime.append(nFile)
               nFile+=1
               if (nFile > maxNumFile):
                    print ("maxNumFile reached")
                    break

        mod[ locdirList2[i] ].update( {'par': dat.par  })

        mod[ locdirList2[i] ]['par'].update( {'dt_bin': dat.dt_bin })

    return(mod)

def plotOnePane(ax, scale,Ncol_min, Ncol_max):

    Nd = len(locdirList2)

    Na = len(mdat[locdirList2[0]] ['ang'][0])
    Ny = len(mdat[locdirList2[0]] ['tau'])

    Nm=0
    distribFun1 = zeros(Na)
    distribFun2 = zeros(Na)
    col_max=0
    col_min = 1e30

    lineType = ['-', '--', '-o', '-*']
    color = ['k', 'b', 'g', 'r']
    markerWidth = [2,2,2,2]

    for i_types in xrange(Nd):

        for j_y in  xrange(Ny):
            for k_ang in  xrange(Na):
                angl = mdat[locdirList2[i_types]] ['ang'][0][k_ang]
                tau= mdat[locdirList2[i_types]] ['tau'][j_y][k_ang]
                col= mdat[locdirList2[i_types]] ['cdens'][j_y][k_ang]

                if col > col_max:
                    col_max = col
                if col <= col_min:
                    col_min = col
                Nm +=1
                if tau>1.:
                    distribFun1[k_ang] +=1

        NBINS = 10
        colDens, dColdens = nm.linspace(col_min, col_max, NBINS, retstep=True)

        NANG = 50
        angle = nm.linspace(0., nm.pi, NANG, retstep=True)

        ang_scat = []
        Ncol_scat = []

        simTime = []

        told  = 0
        dt_bin = mdat[locdirList2[0]] ['par']['dt_bin']

        for j_y in  xrange(Ny):

            tnew = told + dt_bin
            simTime.append(tnew)
            told=tnew

            for k_ang in  xrange(Na):

                angl = mdat[locdirList2[i_types]] ['ang'][0][k_ang]
                col= mdat[locdirList2[i_types]] ['cdens'][j_y][k_ang]

                scale_m = 1.+  (col - Ncol_min)/(Ncol_max-Ncol_min)*(scale -1)
                col /= scale_m

                Ncol_scat.append(col)
                ang_scat.append(angl)

        x = nm.array(mdat[locdirList2[0]]['ang'][0])
        imax=  distribFun1.argmax()
        thmax = x[imax] - nm.pi/2.
        x=x-thmax

#         print(thmax)

        loc1='lower center'
        simTimeArr =nm.array(simTime)

        print (len(Ncol_scat))
#         eps = 1.e-2
#         Ncol_scat[Ncol_scat<eps]=eps

        if i_types==0:

#             color = [str(y/100. ) for y in simTimeArr]
#             color = matplotlib.cm.rainbow(color)
#             print color
#             color = [simTime,simTime]
#             x = np.random.rand(100)
#             y = np.random.rand(100)
#             t=x
#             print t
#             plt.scatter(x, y, c=t)
#             plt.show()

            print (nm.array(ang_scat))

            x1 =(nm.array(ang_scat))*180./nm.pi - 0

            y1= log10( Ncol_scat )

#             print  len(x),len(y), len(simTime);exit()

            cm = plt.cm.get_cmap('RdYlBu')
            ax.scatter( x1, y1, cmap=cm)

#             .xlim((0,185))

#             fig.colorbar(sc)
#             ax1.scatter( x,y, c=simTime)
# #
#         ax1 = fig.add_subplot(131)
#         ax1.plot(x*180./nm.pi, log10(distribFun1),  lineType[i_types], color=color[i_types], linewidth=markerWidth[i_types]

#         ax1.legend(('$\Gamma=0.01$', '$\Gamma=0.05$', '$\Gamma=0.1$', '$\Gamma=0.3$'),
#                         shadow = False, loc = loc1)
        if i_types==1:
            ax2 = fig.add_subplot(142,sharey=ax1)
            ax2.scatter((nm.array(ang_scat)-thmax)*180./nm.pi, log10(Ncol_scat), color=color[i_types])

#         ax2.legend(('$\Gamma=0.01$', '$\Gamma=0.05$', '$\Gamma=0.1$', '$\Gamma=0.3$'),
#                         shadow = False, loc = loc1)
        if i_types==2:
            ax3 = fig.add_subplot(143,sharey=ax1)
            ax3.scatter((nm.array(ang_scat)-thmax)*180./nm.pi, log10(Ncol_scat), color=color[i_types])
#         ax2.legend(('$\Gamma=0.01$', '$\Gamma=0.05$', '$\Gamma=0.1$', '$\Gamma=0.3$'),
#                         shadow = False, loc = loc1)
        if i_types==3:
            ax3 = fig.add_subplot(144,sharey=ax1)
            ax3.scatter((nm.array(ang_scat)-thmax)*180./nm.pi, log10(Ncol_scat), color=color[i_types])

# ----------------------------------------------------------------------------
#                                    the MAIN
# ----------------------------------------------------------------------------

whatToDo = 'calculTauAndColDens'

#whatToDo = 'processTauAndColDensFromFile'

dat =ath.athDataModel()

if socket.gethostname()=='atorus':

 #   locDirList = ['/local/data/atorus2/dora/HW_Jan201707_256x8x256_L0.5n10e8']

    locDirList = ['/local/data/atorus1/dora/PROJECTS/AthenaWind']
    basePath = locDirList[0]

    if os.path.isdir( basePath +'/bin' ):
      dataDir = basePath+'/bin/'
      locdirList2 = ['']
      simParamFileDir = basePath+'/tst/cylindrical/'
#      print(simParamFileDir); exit()

    else:
      dataDir = basePath+'/'
      locdirList2 = ['']
      simParamFileDir = basePath+'/'

    if  whatToDo == 'processTauAndColDensFromFile':

        pathToPickledFile = dataDir
        fileNameList = ['multiDat_TauColDensVsAngle.p', 'multiDat_TauColDensVsAngle.p']
        locdirList2 = ['']

    put_out= '/local/data/atorus1/dora/PROJECTS/SCRIPTS/T9'
    put_FIG= '/local/data/atorus1/dora/PROJECTS/SCRIPTS/T9'

else:
    if whatToDo == 'calculTauAndColDens' :
         locdirList = [ 'SolovievSep201615_256x8x256_L0.n10e10/']
         locdirList = [ 'runDec201608_256x8x256_L0.5n10e8/']
         put_out= '/Users/dora/WORK/ECLIPSE_SPACE/torus9'
         put_FIG = '/Users/dora/Documents/TEX/torus9/'
         locdir = locdirList[0]
         dirFileToReadBase = os.getcwd()
         dataDir = '/DATA/'
         dirFileToReadBase = os.getcwd()
         dataDir = '/DATA'
         dirToRead = dirFileToReadBase + dataDir+locdirList[0]
         dat.loadSimulationParam(dirToRead + 'athinput.torus9_hydro_2D', print_res=True)

    elif whatToDo == 'processTauAndColDensFromFile':
        pathToPickledFile = '/Users/dora/WORK/ECLIPSE_SPACE/torus9/'
        fileNameList = ['SOL_multiDat_TauColDensVsAngle.p', 'HW_multiDat_TauColDensVsAngle.p']
        locdirList2 = ['']
        put_FIG = '/Users/dora/Documents/TEX/torus9/'

if whatToDo == 'calculTauAndColDens':

        print('files =', basePath, dataDir,simParamFileDir)

        multiDat=iteratorOverDataDirectoriesOverHDfFiles(basePath, dataDir,simParamFileDir,locdirList2,
                                            transmisionFunctionsAngleGrid, fileToSavePrefix=False)

#         for i_type_mod in xrange(len(locdirList2)):

        fileToSavePrefix ='multiDat_TauColDensVsAngle.p'
        filename = dataDir + fileToSavePrefix

        pickle.dump(multiDat, open(filename, "wb"))

        # try:
        #     nm.savetxt(filename,  list(multiDat))
        #     print("saving to ",  filename)
        # except IOError:
        #     print('cannot save to', filename)

        multiDat=[]

        print("calculTauAndColDens ..done");
        exit()

if whatToDo =='processTauAndColDensFromFile':

    fileName = fileNameList[0]
    filename = pathToPickledFile + fileName

    fig, ax = plt.subplots(1, 2, sharey=True)

    for filename, np in zip(fileNameList, range(len(fileNameList))):
        mdat = pickle.load( open( filename, "rb" ) )

        Ncol_min  = 10**21
        Ncol_max  = 10**27.4

        if np==0:
            scale = 1
        else:
            scale =12

        plotOnePane(ax[np],scale, Ncol_min, Ncol_max)

    plotFormat(ax[np])
    fig.suptitle('Column density $(cm^{-2})$', y=0.95, fontsize=16)

    fig.text(0.5, 0.02, 'Inclination angle', ha='center', fontsize=16)

    put_out= '/Users/dora/Documents/TEX/torus9/'
    fileNameToSave = put_out+'colDensInclAngleAllModels'
#     fig.savefig(fileNameToSave + ".pdf", format='pdf')

    show()

print('END OF PROGRAM')

#                 idxa = (np.abs(angle-angl)).argmin()
#                 idxc = (np.abs(colDens-col)).argmin()

#     Ncol = nm.sort(distribFun2)
#     indx = nm.argsort(distribFun2)
#     for i in indx:
#     cmin = distribFun2[0]
#     cmax = distribFun2[len(distribFun2)-1]

#     for i_type_mod in xrange(len(locdirList2)):
#         print("model", i_type_mod)
#         for yin_dir, j in zip(mdat[locdirList2[i_type_mod]]['tau'], xrange(N)):
#             ax.plot(x[0], log10(yin_dir))