'''
Created on Aug 21, 2016
@author: Burkhard
'''
#======================
# imports
#======================
import tkinter as tk
from tkinter import Menu
from tkinter import ttk

#======================
# functions
#======================
# Exit GUI cleanly
def _quit():
    win.quit()
    win.destroy()
    exit()

#======================
# procedural code
#======================
# Create instance
win = tk.Tk()

# Add a title
win.title("Python Projects")
# ---------------------------------------------------------------
# Creating a Menu Bar
menuBar = Menu()
win.config(menu=menuBar)

# Add menu items
fileMenu = Menu(menuBar, tearoff=0)
fileMenu.add_command(label="New")
fileMenu.add_separator()
fileMenu.add_command(label="Exit", command=_quit)
menuBar.add_cascade(label="File", menu=fileMenu)

# Add another Menu to the Menu Bar and an item
helpMenu = Menu(menuBar, tearoff=0)
helpMenu.add_command(label="About")
menuBar.add_cascade(label="Help", menu=helpMenu)
# ---------------------------------------------------------------

# Tab Control / Notebook
tabControl = ttk.Notebook(win)          # Create Tab Control

tab1 = ttk.Frame(tabControl)            # Create a tab
tabControl.add(tab1, text='NOAA')       # Add the tab

tab2 = ttk.Frame(tabControl)                # Add a second tab
tabControl.add(tab2, text='Station IDs')    # Make second tab visible

tab3 = ttk.Frame(tabControl)            # Add a second tab
tabControl.add(tab3, text='Images')     # Make second tab visible

tab4 = ttk.Frame(tabControl)            # Add a second tab
tabControl.add(tab4, text='OpenWeatherMap')      # Make second tab visible

tabControl.pack(expand=1, fill="both")  # Pack to make visible
# ---------------------------------------------------------------

# We are creating a container frame to hold all other widgets
weather_conditions_frame = ttk.LabelFrame(tab1, text=' Current Weather Conditions ')
weather_conditions_frame.grid(column=0, row=1, padx=8, pady=4)

#================
ENTRY_WIDTH = 25
#================

# Adding Label & Textbox Entry widgets
#---------------------------------------------
ttk.Label(weather_conditions_frame, text="Last Updated:").grid(column=0, row=1, sticky='E')         # <== right-align
updated = tk.StringVar()
updatedEntry = ttk.Entry(weather_conditions_frame, width=ENTRY_WIDTH, textvariable=updated, state='readonly')
updatedEntry.grid(column=1, row=1, sticky='W')
#---------------------------------------------
ttk.Label(weather_conditions_frame, text="Weather:").grid(column=0, row=2, sticky='E')               # <== increment row for each
weather = tk.StringVar()
weatherEntry = ttk.Entry(weather_conditions_frame, width=ENTRY_WIDTH, textvariable=weather, state='readonly')
weatherEntry.grid(column=1, row=2, sticky='W')                                  # <== increment row for each
#---------------------------------------------
ttk.Label(weather_conditions_frame, text="Temperature:").grid(column=0, row=3, sticky='E')
temp = tk.StringVar()
tempEntry = ttk.Entry(weather_conditions_frame, width=ENTRY_WIDTH, textvariable=temp, state='readonly')
tempEntry.grid(column=1, row=3, sticky='W')
#---------------------------------------------
ttk.Label(weather_conditions_frame, text="Dewpoint:").grid(column=0, row=4, sticky='E')
dew = tk.StringVar()
dewEntry = ttk.Entry(weather_conditions_frame, width=ENTRY_WIDTH, textvariable=dew, state='readonly')
dewEntry.grid(column=1, row=4, sticky='W')
#---------------------------------------------
ttk.Label(weather_conditions_frame, text="Relative Humidity:").grid(column=0, row=5, sticky='E')
rel_humi = tk.StringVar()
rel_humiEntry = ttk.Entry(weather_conditions_frame, width=ENTRY_WIDTH, textvariable=rel_humi, state='readonly')
rel_humiEntry.grid(column=1, row=5, sticky='W')
#---------------------------------------------
ttk.Label(weather_conditions_frame, text="Wind:").grid(column=0, row=6, sticky='E')
wind = tk.StringVar()
windEntry = ttk.Entry(weather_conditions_frame, width=ENTRY_WIDTH, textvariable=wind, state='readonly')
windEntry.grid(column=1, row=6, sticky='W')
#---------------------------------------------
ttk.Label(weather_conditions_frame, text="Visibility:").grid(column=0, row=7, sticky='E')
visi = tk.StringVar()
visiEntry = ttk.Entry(weather_conditions_frame, width=ENTRY_WIDTH, textvariable=visi, state='readonly')
visiEntry.grid(column=1, row=7, sticky='W')
#---------------------------------------------
ttk.Label(weather_conditions_frame, text="MSL Pressure:").grid(column=0, row=8, sticky='E')
msl = tk.StringVar()
mslEntry = ttk.Entry(weather_conditions_frame, width=ENTRY_WIDTH, textvariable=msl, state='readonly')
mslEntry.grid(column=1, row=8, sticky='W')
#---------------------------------------------
ttk.Label(weather_conditions_frame, text="Altimeter:").grid(column=0, row=9, sticky='E')
alti = tk.StringVar()
altiEntry = ttk.Entry(weather_conditions_frame, width=ENTRY_WIDTH, textvariable=alti, state='readonly')
altiEntry.grid(column=1, row=9, sticky='E')
#---------------------------------------------

# Add some space around each widget
for child in weather_conditions_frame.winfo_children():
        child.grid_configure(padx=4, pady=2)

#########################################################################################
# NOAA (National Oceanic and Atmospheric Administration) section starts here

# We are creating a container frame to hold other widgets
weather_cities_frame = ttk.LabelFrame(tab1, text=' Latest Observation for ')
weather_cities_frame.grid(column=0, row=0, padx=8, pady=4)

# ---------------------------------------------------------------
# Adding a Label
ttk.Label(weather_cities_frame, text="Weather Station ID: ").grid(column=0, row=0) # empty space for alignment

# ---------------------------------------------------------------
station_id = tk.StringVar()
station_id_combo = ttk.Combobox(weather_cities_frame, width=6, textvariable=station_id)
                        # Los Angeles, Denver, New York City
station_id_combo['values'] = ('KLAX', 'KDEN', 'KNYC')
station_id_combo.grid(column=1, row=0)
station_id_combo.current(0)                 # highlight first city station id

# ---------------------------------------------------------------
# callback function
def _get_station():
    station = station_id_combo.get()
    get_weather_data(station)
    populate_gui_from_dict()

get_weather_btn = ttk.Button(weather_cities_frame,text='Get Weather', command=_get_station).grid(column=2, row=0)

# Station City label
location = tk.StringVar()
ttk.Label(weather_cities_frame, textvariable=location).grid(column=0, row=1, columnspan=3)
# ---------------------------------------------------------------

for child in weather_cities_frame.winfo_children():
        child.grid_configure(padx=5, pady=4)

#########################################################################################
# NOAA DATA directly from live web search

#Retrieve the tags we are interested in
weather_data_tags_dict = {
    'observation_time': '',
    'weather': '',
    'temp_f':  '',
    'temp_c':  '',
    'dewpoint_f': '',
    'dewpoint_c': '',
    'relative_humidity': '',
    'wind_string':   '',
    'visibility_mi': '',
    'pressure_string': '',
    'pressure_in': '',
    'location': ''
    }

# ---------------------------------------------------------------
import urllib.request

def get_weather_data(station_id='KLAX'):
    url_general = 'http://www.weather.gov/xml/current_obs/{}.xml'
    url = url_general.format(station_id)
#     print(url)
    request = urllib.request.urlopen( url )
    content = request.read().decode()
#     print(content)

    # Using ElementTree to retrieve specific tags from the xml
    import xml.etree.ElementTree as ET
    xml_root = ET.fromstring(content)
#     print('xml_root: {}\n'.format(xml_root.tag))

    for data_point in weather_data_tags_dict.keys():
        weather_data_tags_dict[data_point] = xml_root.find(data_point).text

#     for key, value in weather_data_tags_dict.items():
#         print(key, value)

# ---------------------------------------------------------------
def populate_gui_from_dict():
    location.set(weather_data_tags_dict['location'])
    updated.set(weather_data_tags_dict['observation_time'].replace('Last Updated on ', ''))
    weather.set(weather_data_tags_dict['weather'])
    temp.set('{} \xb0F  ({} \xb0C)'.format(weather_data_tags_dict['temp_f'], weather_data_tags_dict['temp_c']))
    dew.set('{} \xb0F  ({} \xb0C)'.format(weather_data_tags_dict['dewpoint_f'], weather_data_tags_dict['dewpoint_c']))
    rel_humi.set(weather_data_tags_dict['relative_humidity'] + ' %')
    wind.set(weather_data_tags_dict['wind_string'])
    visi.set(weather_data_tags_dict['visibility_mi'] + ' miles')
    msl.set(weather_data_tags_dict['pressure_string'])
    alti.set(weather_data_tags_dict['pressure_in'] + ' in Hg')

#########################################################################################
# TAB 2
#######
# We are creating a container frame to hold all other widgets
weather_states_frame = ttk.LabelFrame(tab2, text=' Weather Station IDs ')
weather_states_frame.grid(column=0, row=0, padx=8, pady=4)

# ---------------------------------------------------------------
# Adding a Label
ttk.Label(weather_states_frame, text="Select a State: ").grid(column=0, row=0) # empty space for alignment

# ---------------------------------------------------------------
state = tk.StringVar()
state_combo = ttk.Combobox(weather_states_frame, width=5, textvariable=state)
state_combo['values'] = ('AL','AK','AZ','AR','CA','CO','CT','DE','FL','GA','HI',
                         'ID','IL','IN','IA','KS','KY','LA','ME','MD','MA','MI',
                         'MN','MS','MO','MT','NE','NV','NH','NJ','NM','NY','NC',
                         'ND','OH','OK','OR','PA','RI','SC','SD','TN','TX','UT',
                         'VT','VA','WA','WV','WI','WY'
                        )
state_combo.grid(column=1, row=0)
state_combo.current(0)                 # highlight first

# ---------------------------------------------------------------
# callback function
def _get_cities():
    state = state_combo.get()
    get_city_station_ids(state)

get_weather_btn = ttk.Button(weather_states_frame,text='Get Cities', command=_get_cities).grid(column=2, row=0)

from tkinter import scrolledtext
scr = scrolledtext.ScrolledText(weather_states_frame, width=30, height=17, wrap=tk.WORD)
scr.grid(column=0, row=1, columnspan=3)

# ---------------------------------------------------------------
for child in weather_states_frame.winfo_children():
        child.grid_configure(padx=6, pady=6)

# ---------------------------------------------------------------
def get_city_station_ids(state='ca'):
    url_general = 'http://w1.weather.gov/xml/current_obs/seek.php?state={}&Find=Find'
    state = state.lower()                       # has to be in lower case
    url = url_general.format(state)
    request = urllib.request.urlopen( url )
    content = request.read().decode()
#     print(content)

    parser = WeatherHTMLParser()
    parser.feed(content)

    # verify we have as many stations as cities
#     print(len(parser.stations) == len(parser.cities))

    scr.delete('1.0', tk.END)  # clear scrolledText widget for next btn click

    for idx in range(len(parser.stations)):
        city_station = parser.cities[idx] + ' (' + parser.stations[idx] + ')'
#         print(city_station)
        scr.insert(tk.INSERT, city_station + '\n')

#     print(parser.stations)
#     print(parser.cities)

from html.parser import HTMLParser

class WeatherHTMLParser(HTMLParser):
    def __init__(self):
        super().__init__()
        self.stations = []
        self.cities = []
        self.grab_data = False

    def handle_starttag(self, tag, attrs):
        for attr in attrs:
            if "display.php?stid=" in str(attr):
                cleaned_attr= str(attr).replace("('href', 'display.php?stid=", '').replace("')", '')
                self.stations.append(cleaned_attr)
                self.grab_data = True

    def handle_data(self, data):
        if self.grab_data:
            self.cities.append(data)
            self.grab_data = False

#########################################################################################
# TAB 3
#######
# We are creating a container frame to hold all other widgets
weather_images_frame = ttk.LabelFrame(tab3, text=' Weather Images ')
weather_images_frame.grid(column=0, row=0, padx=8, pady=4)

# ---------------------------------------------------------------
# requires Pillow (PIL in Python 2.x)
import PIL.Image
import PIL.ImageTk

im = PIL.Image.open("few_clouds.png")
photo = PIL.ImageTk.PhotoImage(im)
ttk.Label(weather_images_frame, image=photo).grid(column=0, row=0)

im = PIL.Image.open("night_few_clouds.png")
photo1 = PIL.ImageTk.PhotoImage(im)
ttk.Label(weather_images_frame, image=photo1).grid(column=1, row=0)

im = PIL.Image.open("night_fair.png")
photo2 = PIL.ImageTk.PhotoImage(im)
ttk.Label(weather_images_frame, image=photo2).grid(column=2, row=0)

#########################################################################################
# TAB 4 OpenWeatherMap
######################

# We are creating a container frame to hold other widgets
open_weather_cities_frame = ttk.LabelFrame(tab4, text=' Latest Observation for ')
open_weather_cities_frame.grid(column=0, row=0, padx=8, pady=4)

# Station City label
open_location = tk.StringVar()
ttk.Label(open_weather_cities_frame, textvariable=open_location).grid(column=0, row=1, columnspan=3)

# ---------------------------------------------------------------
# Adding a Label
ttk.Label(open_weather_cities_frame, text="City: ").grid(column=0, row=0)

# ---------------------------------------------------------------
open_city = tk.StringVar()
open_city_combo = ttk.Combobox(open_weather_cities_frame, width=16, textvariable=open_city)
open_city_combo['values'] = ('Los Angeles, US', 'London, UK', 'Paris, FR', 'Mumbai, IN', 'Beijing, CN')
open_city_combo.grid(column=1, row=0)
open_city_combo.current(0)                 # highlight first city station id

# ---------------------------------------------------------------
# callback function
def _get_station_open():
    city = open_city_combo.get()
    get_open_weather_data(city)

get_weather_btn = ttk.Button(open_weather_cities_frame,text='Get Weather', command=_get_station_open).grid(column=2, row=0)

# ---------------------------------------------------------------
for child in open_weather_cities_frame.winfo_children():
        child.grid_configure(padx=5, pady=2)

# ---------------------------------------------------------------
# We are creating a container frame to hold all other widgets
open_weather_conditions_frame = ttk.LabelFrame(tab4, text=' Current Weather Conditions ')
open_weather_conditions_frame.grid(column=0, row=1, padx=8, pady=4)

#================
ENTRY_WIDTH = 25
#================

# Adding Label & Textbox Entry widgets
#---------------------------------------------
ttk.Label(open_weather_conditions_frame, text="Last Updated:").grid(column=0, row=1, sticky='E')         # <== right-align
open_updated = tk.StringVar()
open_updatedEntry = ttk.Entry(open_weather_conditions_frame, width=ENTRY_WIDTH, textvariable=open_updated, state='readonly')
open_updatedEntry.grid(column=1, row=1, sticky='W')
#---------------------------------------------
ttk.Label(open_weather_conditions_frame, text="Weather:").grid(column=0, row=2, sticky='E')               # <== increment row for each
open_weather = tk.StringVar()
open_weatherEntry = ttk.Entry(open_weather_conditions_frame, width=ENTRY_WIDTH, textvariable=open_weather, state='readonly')
open_weatherEntry.grid(column=1, row=2, sticky='W')                                  # <== increment row for each
#---------------------------------------------
ttk.Label(open_weather_conditions_frame, text="Temperature:").grid(column=0, row=3, sticky='E')
open_temp = tk.StringVar()
open_tempEntry = ttk.Entry(open_weather_conditions_frame, width=ENTRY_WIDTH, textvariable=open_temp, state='readonly')
open_tempEntry.grid(column=1, row=3, sticky='W')
#---------------------------------------------
# ttk.Label(open_weather_conditions_frame, text="Dewpoint:").grid(column=0, row=4, sticky='E')
# dew = tk.StringVar()
# dewEntry = ttk.Entry(open_weather_conditions_frame, width=ENTRY_WIDTH, textvariable=dew, state='readonly')
# dewEntry.grid(column=1, row=4, sticky='W')
#---------------------------------------------
ttk.Label(open_weather_conditions_frame, text="Relative Humidity:").grid(column=0, row=5, sticky='E')
open_rel_humi = tk.StringVar()
open_rel_humiEntry = ttk.Entry(open_weather_conditions_frame, width=ENTRY_WIDTH, textvariable=open_rel_humi, state='readonly')
open_rel_humiEntry.grid(column=1, row=5, sticky='W')
#---------------------------------------------
ttk.Label(open_weather_conditions_frame, text="Wind:").grid(column=0, row=6, sticky='E')
open_wind = tk.StringVar()
open_windEntry = ttk.Entry(open_weather_conditions_frame, width=ENTRY_WIDTH, textvariable=open_wind, state='readonly')
open_windEntry.grid(column=1, row=6, sticky='W')
#---------------------------------------------
ttk.Label(open_weather_conditions_frame, text="Visibility:").grid(column=0, row=7, sticky='E')
open_visi = tk.StringVar()
open_visiEntry = ttk.Entry(open_weather_conditions_frame, width=ENTRY_WIDTH, textvariable=open_visi, state='readonly')
open_visiEntry.grid(column=1, row=7, sticky='W')
#---------------------------------------------
ttk.Label(open_weather_conditions_frame, text="Pressure:").grid(column=0, row=8, sticky='E')
open_msl = tk.StringVar()
open_mslEntry = ttk.Entry(open_weather_conditions_frame, width=ENTRY_WIDTH, textvariable=open_msl, state='readonly')
open_mslEntry.grid(column=1, row=8, sticky='W')
#---------------------------------------------
ttk.Label(open_weather_conditions_frame, text="Sunrise:").grid(column=0, row=9, sticky='E')
sunrise = tk.StringVar()
sunriseEntry = ttk.Entry(open_weather_conditions_frame, width=ENTRY_WIDTH, textvariable=sunrise, state='readonly')
sunriseEntry.grid(column=1, row=9, sticky='E')
#---------------------------------------------
ttk.Label(open_weather_conditions_frame, text="Sunset:").grid(column=0, row=10, sticky='E')
sunset = tk.StringVar()
sunsetEntry = ttk.Entry(open_weather_conditions_frame, width=ENTRY_WIDTH, textvariable=sunset, state='readonly')
sunsetEntry.grid(column=1, row=10, sticky='E')
#---------------------------------------------

# Add some space around each widget
for child in open_weather_conditions_frame.winfo_children():
        child.grid_configure(padx=4, pady=2)

#########################################################################################
# OpenWeatherMap Data collection

from Section1_Video4.API_key import OWM_API_KEY
from urllib.request import urlopen
import json

def get_open_weather_data(city='London,uk'):
    city = city.replace(' ', '%20')
    url = "http://api.openweathermap.org/data/2.5/weather?q={}&appid={}".format(city, OWM_API_KEY)
    response = urlopen(url)
    data = response.read().decode()
    json_data = json.loads(data)

    from pprint import pprint
    pprint(json_data)

    lat_long = json_data['coord']
    lastupdate_unix = json_data['dt']
    city_id = json_data['id']
    humidity = json_data['main']['humidity']
    pressure = json_data['main']['pressure']
    temp_kelvin = json_data['main']['temp']
    city_name = json_data['name']
    city_country = json_data['sys']['country']
    sunrise_unix = json_data['sys']['sunrise']
    sunset_unix = json_data['sys']['sunset']
    try: visibility_meter = json_data['visibility']
    except: visibility_meter = 'N/A'
    owm_weather = json_data['weather'][0]['description']
    weather_icon = json_data['weather'][0]['icon']
    wind_deg = json_data['wind']['deg']
    wind_speed_meter_sec = json_data['wind']['speed']

    def kelvin_to_celsius(temp_k):
        return "{:.1f}".format(temp_k - 273.15)

    def kelvin_to_fahrenheit(temp_k):
        return "{:.1f}".format((temp_k - 273.15)* 1.8000 + 32.00)

    from datetime import datetime
    def unix_to_datetime(unix_time):
        return datetime.fromtimestamp(int(unix_time)
        ).strftime('%Y-%m-%d %H:%M:%S')

    def meter_to_miles(meter):
        return "{:.2f}".format((meter * 0.00062137))

    if visibility_meter is 'N/A':
        visibility_miles = 'N/A'
    else:
        visibility_miles = meter_to_miles(visibility_meter)

    def mps_to_mph(meter_second):
        return "{:.1f}".format((meter_second * (2.23693629)))

    # -------------------------------------------------------
    # Update GUI entry widgets with live data
    open_location.set('{}, {}'.format(city_name, city_country))

    lastupdate = unix_to_datetime(lastupdate_unix)
    open_updated.set(lastupdate)
    open_weather.set(owm_weather)
    temp_fahr = kelvin_to_fahrenheit(temp_kelvin)
    temp_cels = kelvin_to_celsius(temp_kelvin)
    open_temp.set('{} \xb0F  ({} \xb0C)'.format(temp_fahr, temp_cels))
    open_rel_humi.set('{} %'.format(humidity))
    wind_speed_mph = mps_to_mph(wind_speed_meter_sec)
    open_wind.set('{} degrees at {} MPH'.format(wind_deg, wind_speed_mph))
    open_visi.set('{} miles'.format(visibility_miles))
    open_msl.set('{} hPa'.format(pressure))
    sunrise_dt = unix_to_datetime(sunrise_unix)
    sunrise.set(sunrise_dt)
    sunset_dt = unix_to_datetime(sunset_unix)
    sunset.set(sunset_dt)

    print(weather_icon)
    url_icon = "http://openweathermap.org/img/w/{}.png".format(weather_icon)
    ico = urlopen(url_icon)
    open_im = PIL.Image.open(ico)
    open_photo = PIL.ImageTk.PhotoImage(open_im)
    ttk.Label(open_weather_cities_frame, image=open_photo).grid(column=0, row=1)
    win.update()        # required or we won't see the icon

#======================
# Start GUI
#======================
# get_weather_data()
# populate_gui_from_dict()
win.mainloop()

_99_DesignWindowsApplication/Exercise Files/Section4 - Creating a GUI application with Tkinter/Section4_Video4/GUI_Weather_OpenWeatherMap.py


from collections import namedtuple
from smoke.util import enum

Type = enum(
    Int       = 0, Float  = 1, Vector = 2,
    VectorXY  = 3, String = 4, Array  = 5,
    DataTable = 6, Int64  = 7)

Flag = enum(
    Unsigned              = 1 <<  0, Coord                   = 1 <<  1,
    NoScale               = 1 <<  2, RoundDown               = 1 <<  3,
    RoundUp               = 1 <<  4, Normal                  = 1 <<  5,
    Exclude               = 1 <<  6, XYZE                    = 1 <<  7,
    InsideArray           = 1 <<  8, ProxyAlways             = 1 <<  9,
    VectorElem            = 1 << 10, Collapsible             = 1 << 11,
    CoordMP               = 1 << 12, CoordMPLowPrecision     = 1 << 13,
    CoordMPIntegral       = 1 << 14, CellCoord               = 1 << 15,
    CellCoordLowPrecision = 1 << 16, CellCoordIntegral       = 1 << 17,
    ChangesOften          = 1 << 18, EncodedAgainstTickcount = 1 << 19)

Prop = namedtuple('Prop',
    'src, name, type, flags, pri, len, bits, dt, low, high, array_prop')


def if_odd(n1):
    if n % 2 !=0:
        #print("done 1")
        return True

def check(n1):
    binary= bin(n)[2:]
    return binary == binary[::-1]

n=int(input())
rus1= check(n)
rus2=if_odd(n)
if rus1 == True and rus2 == True:
    print("YES")
else:
    print("NO")

Codeforces/Assiut University Training - sheets/Sheet 5 Function/C._Wonderful_Number.py


#!c:/python26/python.exe
#
# need PIL
# see
# http://www.pythonware.com/products/pil/
# http://www.pythonware.com/library/pil/handbook/introduction.htm
# http://www.pythonware.com/library/pil/handbook/image.htm
# http://www.riaidea.com/blog/archives/279.html
#
from PIL import Image

def decomposejpg(inputName, jpgName, horizontal):
	im = Image.open(inputName)
	im.load()
	if horizontal:
		outim = Image.new("RGB", (im.size[0] * 2, im.size[1]))
	else:
		outim = Image.new("RGB", (im.size[0], im.size[1] * 2))
	outim.paste(im, (0, 0))
	source = im.split()
	source[0].paste(source[0].point(lambda i: 0xff), None)
	im = Image.merge(im.mode, (source[3], source[3], source[3], source[0]))
	if horizontal:
		outim.paste(im, (im.size[0], 0))
	else:
		outim.paste(im, (0, im.size[1]))
	outim.save(jpgName)

def composejpg(jpgName, pngName, horizontal):
	jpgIm = Image.open(jpgName)
	jpgIm.load()
	if horizontal:
		outim = Image.new("RGBA", (jpgIm.size[0] / 2, jpgIm.size[1]))
		img1 = jpgIm.crop((0, 0, jpgIm.size[0] / 2, jpgIm.size[1])).split()
		img2 = jpgIm.crop((jpgIm.size[0] / 2, 0, jpgIm.size[0], jpgIm.size[1])).split()
	else:
		outim = Image.new("RGBA", (jpgIm.size[0], jpgIm.size[1] / 2))
		img1 = jpgIm.crop((0, 0, jpgIm.size[0], jpgIm.size[1] / 2)).split()
		img2 = jpgIm.crop((0, jpgIm.size[1] / 2, jpgIm.size[0], jpgIm.size[1])).split()
	im = Image.merge("RGBA", (img1[0], img1[1], img1[2], img2[0]))
	im.save(pngName)

horizontal = 1
decomposejpg("preview.png", "out.jpg", horizontal)
composejpg("out.jpg", "out.png", horizontal)


import tmdbsimple as tmdb
import pandas as pd

# Prompt the user for their API key
api_key = input("Enter your TMDB API key: ")

# Set up the TMDB client with the API key
tmdb.API_KEY = api_key

# Ask the user whether they want to download their watchlist or a specific list by ID
list_type = input("Do you want to download your watchlist (w) or a specific list by ID (l)? ")

if list_type == "w":
    # Download the user's watchlist
    account = tmdb.Account()
    watchlist = account.watchlist().get(language="en-US", page=1)
    list_name = "watchlist"
else:
    # Ask the user for the list ID
    list_id = input("Enter the ID of the list you want to download: ")
    # Download the specified list
    client = tmdb.Client(api_key)
    list_details = client.lists.get(list_id)
    list_items = client.lists.items(list_id, language="en-US")
    list_name = list_details["name"]
    watchlist = list_items["results"]

# Convert the watchlist to a Pandas DataFrame
df = pd.DataFrame(watchlist)

# Write the DataFrame to an Excel file
filename = f"{list_name}.xlsx"
df.to_excel(filename, index=False)

print(f"{len(watchlist)} items written to {filename}")


from ctypes import c_uint32
from multiprocessing import Pipe, Value
from multiprocessing.connection import Connection as PipeConnection

from py_mpworker import MPWorker

TEST_INTERVAL = 1

def process_event(
    state: Value, desired_call_count: int, finished_signaler: PipeConnection,
):
    with state.get_lock():
        state.value += 1
        if state.value == desired_call_count:
            finished_signaler.send(None)

def test_mpworker_callback_should_be_called_multiple_times():
    desired_call_count = 5
    (signal_receiver, finished_signaler) = Pipe(duplex=False)
    shared_state = Value(c_uint32)
    shared_state.value = 0

    def callback(state: Value):
        process_event(state, desired_call_count, finished_signaler)

    worker = MPWorker("test_worker", TEST_INTERVAL, callback, shared_state)
    worker.start()
    signal_receiver.poll(3600)
    worker.stop()
    with shared_state.get_lock():
        assert shared_state.value == desired_call_count


from rest_framework.decorators import api_view
from rest_framework.response import Response
from CRUD import models
from . import serializer
from django.core.exceptions import ObjectDoesNotExist

# GET , POST , DELETE , PUT

@api_view(['GET', 'POST'])
def With_NOID(request):

    if request.method == 'GET':
        accounts = models.Accounts.objects.all()
        serializeData = serializer.AccountSerializer(accounts, many=True)

        response = Response()

        data = {
            'isError': False,
            'Message': serializeData.data
        }

        response.data = data

    elif request.method == 'POST':

        # Check email address
        try:
            user = models.Accounts.objects.get(Email=request.POST.get('Email'))

            response = Response()

            data = {
                'isError': True,
                'Message': "Email is already in use"
            }

            response.data = data

        except ObjectDoesNotExist:
            try:
                user = models.Accounts.objects.get(
                    Username=request.POST.get('Username'))

                response = Response()

                data = {
                    'isError': True,
                    'Message': "Username is already in use"
                }

                response.data = data
            except ObjectDoesNotExist:
                serializeData = serializer.AccountSerializer(data=request.data)

                if serializeData.is_valid():
                    serializeData.save()

                    response = Response()

                    data = {
                        'isError': False,
                        'Message': "New Account Has Been Created"
                    }

                    response.data = data

    return response

@api_view(['GET', 'PUT', 'DELETE'])
def With_ID(request, pk):

    if request.method == 'GET':
        try:
            account = models.Accounts.objects.get(id=pk)
            serializeData = serializer.AccountSerializer(account)

            response = Response()

            data = {
                'isError': False,
                'Message': serializeData.data
            }

            response.data = data
        except ObjectDoesNotExist:
            response = Response()

            data = {
                'isError': True,
                'Message': "Account Was Not Created"
            }

            response.data = data

    elif request.method == 'DELETE':
        try:
            account = models.Accounts.objects.get(id=pk)
            account.delete()

            response = Response()

            data = {
                'isError': False,
                'Message': "Account Has been Deleted"
            }

            response.data = data

        except ObjectDoesNotExist:
            response = Response()

            data = {
                'isError': True,
                'Message': "Account Was Not Created"
            }

            response.data = data

    elif request.method == 'PUT':
        try:
            account = models.Accounts.objects.get(id=pk)

            serializeData = serializer.AccountSerializer(account, request.data)

            if serializeData.is_valid():
                serializeData.save()

                response = Response()

                data = {
                    'isError': False,
                    'Message': "New Account Has Been Updated"
                }

                response.data = data

        except ObjectDoesNotExist:
            response = Response()

            data = {
                'isError': True,
                'Message': "Account Was Not Created"
            }

            response.data = data

    return response


import json, string, pickle
from gensim.scripts.glove2word2vec import glove2word2vec
from gensim.models.keyedvectors import KeyedVectors
# from nltk.tokenize import WordPunctTokenizer
from nltk.tokenize import word_tokenize
import numpy as np

WORD_DIM = 300

# glove2word2vec(glove_input_file='/Users/Weini/Dev/GloVe/glove.6B/glove.6B.300d.txt', word2vec_output_file='/Users/Weini/Dev/GloVe/glove.6B/word2vec_glove.6B.300d.txt')
# glove_model = KeyedVectors.load_word2vec_format("/Users/Weini/Dev/GloVe/glove.6B/word2vec_glove.6B.300d.txt", binary=False)
# with open('/Users/Weini/Dev/GloVe/glove.6B/word2vec_glove.6B.300d.pkl', 'wb') as f:
#     pickle.dump(glove_model, f)

def build_vocab(json_filename, glove_p_filename, save_vocab='./data/vqa_glove_vocab2ind.pkl', save_embeddings='./data/vqa_glove_embeddings.pkl'):
	vocab = {'NULL': 0}
	vecs = np.zeros(WORD_DIM)

	with open(glove_p_filename,'rb') as f:
		glove_embed = pickle.load(f)

	with open(json_filename) as file:
		json_data = json.load(file)
		for img in json_data['images']:
			for qapair in img['qa_pairs']:
				# qa_id, image_id
				li = []
				li.extend(word_tokenize(qapair['question'].strip(string.punctuation).lower()))
				# q = word_tokenize(qapair['question'].strip(string.punctuation).lower())
				li.extend(word_tokenize(qapair['answer'].strip(string.punctuation).lower()))
				# answers = [word_tokenize(qapair['answer'].strip(string.punctuation).lower())]
				for choice in qapair['multiple_choices']:
					# answers.append(word_tokenize(choice.strip(string.punctuation).lower()))
					li.extend(word_tokenize(choice.strip(string.punctuation).lower()))

				for t in li:
					if t not in vocab:
						if t in glove_embed:
							# vecs.append(glove_embed[t])
							vecs = np.vstack((vecs, glove_embed[t]))
							vocab[t] = vecs.shape[0]-1

	with open(save_vocab, 'wb') as handle:
		pickle.dump(vocab, handle)
	with open(save_embeddings, 'wb') as handle:
		pickle.dump(vecs, handle)

def process_qas_embeds(json_filename, vocab_p_filename, save_file=''):
	with open(vocab_p_filename,'rb') as f:
		vocab = pickle.load(f)

	qa_map = []
	with open(json_filename) as file:
		json_data = json.load(file)
		for img in json_data['images']:
			for qapair in img['qa_pairs']:
				# qa_id, image_id
				q = word_tokenize(qapair['question'].strip(string.punctuation).lower())
				answers = [word_tokenize(qapair['answer'].strip(string.punctuation).lower())]
				for choice in qapair['multiple_choices']:
					answers.append(word_tokenize(choice.strip(string.punctuation).lower()))

				q_ids = []
				as_ids = []
				for t in q:
					id = vocab[t] if t in vocab else 0
					q_ids.append(id)
				q_ids = np.asarray(q_ids, dtype=np.int32)
				for a in answers:
					a_ids = []
					for t in a:
						id = vocab[t] if t in vocab else 0
						a_ids.append(id)
					as_ids.append(np.asarray(a_ids, dtype=np.int32))

				qa = {'qa_id': qapair['qa_id'],
					  'img_id': img["image_id"],
					  'q_embed': q_ids, # nparray of indices
					  'a_embeds': as_ids} # list of 4 nparray of indices
				qa_map.append(qa)
				# print()
		if save_file:
			with open(save_file, 'wb') as handle:
				pickle.dump(qa_map, handle)
		return qa_map

def sent2vec(glove_embed, sentence, word_dim):
	v = np.array([]).reshape(0, word_dim)
	for w in sentence:
		if w in glove_embed:
			v = np.vstack((v, glove_embed[w]))
	if v.shape[0] == 0:
		v = np.vstack((v, np.zeros_like(glove_embed['this'])))
	return v

def process_qas(json_filename, glove_p_filename, save=''):
	with open(glove_p_filename,'rb') as f:
		glove_embed = pickle.load(f)

	qa_map = []
	with open(json_filename) as file:
		json_data = json.load(file)
		for img in json_data['images']:
			for qapair in img['qa_pairs']:
				# qa_id, image_id
				# qa_id_list.append(qapair['qa_id'])
				q = word_tokenize(qapair['question'].strip(string.punctuation).lower())
				answers = [word_tokenize(qapair['answer'].strip(string.punctuation).lower())]
				for choice in qapair['multiple_choices']:
					answers.append(word_tokenize(choice.strip(string.punctuation).lower()))

				# qv = np.array([]).reshape(WORD_DIM,)
				qv = sent2vec(glove_embed, q, WORD_DIM)
				# print("qv", qv.shape)
				# qa_m = np.array([]).reshape(0, WORD_DIM)
				av_list = []
				for ans in answers:
					av = sent2vec(glove_embed, ans, WORD_DIM)
					av_list.append(av)
				qa = {'qa_id': qapair['qa_id'],
						'img_id': img["image_id"],
						'q_embed': qv,
						'a_embeds': av_list}
				qa_map.append(qa)
	if save:
		with open(save, 'wb') as handle:
			pickle.dump(qa_map, handle)
	return qa_map

if __name__ == '__main__':
	json_filename = './data/visual7w-telling.json'
	glove_p_filename = './data/word2vec_glove.6B.300d.pkl'
	# print(process_qas(json_filename, glove_p_filename)[0])

	build_vocab(json_filename, glove_p_filename)

	process_qas_embeds('./data/visual7w-telling_train.json', './data/vqa_glove_vocab2ind.pkl', './data/qa_embeds_train.pkl')
	process_qas_embeds('./data/visual7w-telling_val.json', './data/vqa_glove_vocab2ind.pkl', './data/qa_embeds_val.pkl')
	process_qas_embeds('./data/visual7w-telling_test.json', './data/vqa_glove_vocab2ind.pkl', './data/qa_embeds_test.pkl')

	# print('image_id_list len', len(image_id_list))
	# print('image_id_list min', min(image_id_list))
	# print('image_id_list max', max(image_id_list))
	# print('qa_id_list len', len(qa_id_list))
	# print('qa_id_list min', min(qa_id_list))
	# print('qa_id_list max', max(qa_id_list))


from typing import Union, List
import os
import ptap_writer.utils as utils

TEMPLATE_LOCATION = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'same_different_task_template.html')
assert os.path.exists(TEMPLATE_LOCATION), 'Could not find template at %s' % (TEMPLATE_LOCATION)

TASK_LOCATION = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'same_different_task.js')
assert os.path.exists(TASK_LOCATION), 'Could not find task at %s' % (TASK_LOCATION)

bool2jsbool = lambda b: 'true' if b else 'false'

INDENT_CHARACTER = '    '

INSTRUCTIONS_STRING = """

Thank you for accepting this HIT! This is a task which will involve detecting whether two images are the same, or not.

Two pictures will be shown to you very rapidly (one after the other).

Your task is to say whether the two pictures are <text style="color:green; font-style:oblique">EXACTLY</text> the same, or not.

If there is even a tiny change, press "different". Otherwise, press "same".

Consistently making accurate choices will <text style="color:green; font-style:oblique">increase your bonus payout</text>, but random guessing will lead to <text style="color:red; font-style:oblique">no bonus</text>.

We may apply a "soft block" (using quals) for workers who choose not to follow these instructions. While this will not be reflected in your account's block rate, you may not be able to gain access to future HITs from our lab.

If you encounter a bug (for example, the task freezing), please contact us and let us know. You will receive compensation for your time.

"""

def generate_same_different_task(
        frame_info_sequence, # [{'stimulus_frame_url_sequence':[], 'stimulus_frame_duration_msec_sequence':[]}]
        ground_truth_is_same_sequence,  # [trials]: -1 (no correct choice), 0 (they are different), or 1 (they are the same)
        give_reinforcement_sequence,  # [trials]: bool (whether to provide reinforcement or not)
        reward_duration_msec, # () msec
        punish_duration_msec,
        choice_duration_msec,
        minimal_choice_duration_msec,
        post_stimulus_delay_duration_msec,
        intertrial_delay_duration_msec,
        inter_choice_presentation_delay_msec,
        pre_choice_lockout_delay_duration_msec,
        minimal_gt_performance_for_bonus,
        pseudo_usd_per_gt_correct,
):

    html_string = _generate_same_different_html(
        frame_info_sequence=frame_info_sequence,
        ground_truth_is_same_sequence=ground_truth_is_same_sequence,
        give_reinforcement_sequence = give_reinforcement_sequence,
        reward_duration_msec=reward_duration_msec,
        punish_duration_msec=punish_duration_msec,
        choice_duration_msec=choice_duration_msec,
        minimal_choice_duration_msec=minimal_choice_duration_msec,
        post_stimulus_delay_duration_msec=post_stimulus_delay_duration_msec,
        intertrial_delay_duration_msec=intertrial_delay_duration_msec,
        inter_choice_presentation_delay_msec=inter_choice_presentation_delay_msec,
        pre_choice_lockout_delay_duration_msec=pre_choice_lockout_delay_duration_msec,
        minimal_gt_performance_for_bonus=minimal_gt_performance_for_bonus,
        pseudo_usd_per_gt_correct=pseudo_usd_per_gt_correct,
    )
    return html_string

def _generate_same_different_html(
        frame_info_sequence,
        ground_truth_is_same_sequence,
        give_reinforcement_sequence,
        reward_duration_msec,
        punish_duration_msec,
        choice_duration_msec,
        minimal_choice_duration_msec,
        post_stimulus_delay_duration_msec,
        intertrial_delay_duration_msec,
        inter_choice_presentation_delay_msec,
        pre_choice_lockout_delay_duration_msec,
        minimal_gt_performance_for_bonus,
        pseudo_usd_per_gt_correct,
):
    """
    frame_info_sequence, // [{'stimulus_frame_url_sequence':[], 'stimulus_frame_duration_msec_sequence':[]}]
    """

    is_different_url = 'https://s3.amazonaws.com/samedifferentbehavior/task_assets/is_different_choice_image.png'
    is_same_url = 'https://s3.amazonaws.com/samedifferentbehavior/task_assets/is_same_choice_image.png'

    """
    Left choice is always "same"
    Right choice is always "different"
    """
    choice0_image_url_sequence = [is_same_url for _ in frame_info_sequence]
    choice1_image_url_sequence = [is_different_url for _ in frame_info_sequence]

    assert len(set(ground_truth_is_same_sequence).difference({-1, 0, 1})) == 0

    rewarded_choice_sequence = []
    ground_truth_correct_choice_sequence = []
    for i, apply_reward in enumerate(give_reinforcement_sequence):
        if apply_reward:
            assert ground_truth_is_same_sequence[i] != -1
            if ground_truth_is_same_sequence[i] == True:
                rewarded_choice_sequence.append(0)
                ground_truth_correct_choice_sequence.append(0)
            elif ground_truth_is_same_sequence[i] == False:
                rewarded_choice_sequence.append(1)
                ground_truth_correct_choice_sequence.append(1)
            else:
                raise Exception
        else:
            rewarded_choice_sequence.append(-1)

    query_string = ''

    all_urls = set()
    for frame_info in frame_info_sequence:
        frame_urls = frame_info['stimulus_frame_url_sequence']
        [all_urls.add(url) for url in frame_urls]

    for c_url in choice0_image_url_sequence + choice1_image_url_sequence:
        all_urls.add(c_url)
    all_urls = list(all_urls)

    image_url_prefix = os.path.commonprefix(all_urls)

    frame_info_sequence_suffix_form = []
    for i_trial, trial_frame_info in enumerate(frame_info_sequence):
        frame_urls = trial_frame_info['stimulus_frame_url_sequence']
        frame_durations = trial_frame_info['stimulus_frame_duration_msec_sequence']

        assert len(frame_urls) == len(frame_durations)
        for d in frame_durations:
            assert d > (1 / 60 * 1000)

        cur_frame_info_suffix_form = {
            'stimulus_frame_url_suffix_sequence': [url.split(image_url_prefix)[-1] for url in frame_urls],
            'stimulus_frame_duration_msec_sequence':list(frame_durations),
        }
        frame_info_sequence_suffix_form.append(dict(cur_frame_info_suffix_form))

    choice0_url_suffix_sequence = [url.split(image_url_prefix)[-1] for url in choice0_image_url_sequence]
    choice1_url_suffix_sequence = [url.split(image_url_prefix)[-1] for url in choice1_image_url_sequence]

    assert isinstance(image_url_prefix, str)
    sequence_vars = [
        frame_info_sequence_suffix_form,
        choice0_url_suffix_sequence,
        choice1_url_suffix_sequence,
        rewarded_choice_sequence,
        ground_truth_correct_choice_sequence,
    ]

    lengths = [len(var) for var in sequence_vars]
    assert len(set(lengths)) == 1, lengths
    ntrials = lengths[0]
    assert ntrials > 0

    positive_msec_scalars = [
        reward_duration_msec,
        punish_duration_msec,
        choice_duration_msec,
        minimal_choice_duration_msec,
        post_stimulus_delay_duration_msec,
        intertrial_delay_duration_msec,
        inter_choice_presentation_delay_msec,
        pre_choice_lockout_delay_duration_msec,
    ]

    for scalar in positive_msec_scalars:
        assert scalar >= 0

    max_safety = 0.02
    assert 0 <= minimal_gt_performance_for_bonus < 1
    assert 0 <= pseudo_usd_per_gt_correct < max_safety
    assert isinstance(pseudo_usd_per_gt_correct, float)
    assert isinstance(query_string, str)

    trial_info = dict(
                image_url_prefix=image_url_prefix,
                frame_info_sequence=frame_info_sequence_suffix_form,
                ground_truth_correct_choice_sequence=ground_truth_correct_choice_sequence,
                choice0_url_suffix_sequence=choice0_url_suffix_sequence,
                choice1_url_suffix_sequence=choice1_url_suffix_sequence,
                rewarded_choice_sequence=rewarded_choice_sequence,
                reward_duration_msec=reward_duration_msec,
                punish_duration_msec=punish_duration_msec,
                choice_duration_msec=choice_duration_msec,
                minimal_choice_duration_msec=minimal_choice_duration_msec,
                post_stimulus_delay_duration_msec=post_stimulus_delay_duration_msec,
                intertrial_delay_duration_msec=intertrial_delay_duration_msec,
                inter_choice_presentation_delay_msec=inter_choice_presentation_delay_msec,
                pre_choice_lockout_delay_duration_msec=pre_choice_lockout_delay_duration_msec,
                minimal_gt_performance_for_bonus=minimal_gt_performance_for_bonus,
                pseudo_usd_per_gt_correct=pseudo_usd_per_gt_correct,
                query_string=query_string,
    )

    """
    Generate HTML
    """

    javascript_expression = 3 * INDENT_CHARACTER + '{\n'
    for k in trial_info:
        cur_val = trial_info[k]
        if isinstance(cur_val, str):
            val = f'"{cur_val}"'
        elif isinstance(cur_val, bool):
            val = bool2jsbool(cur_val)
        elif isinstance(cur_val, tuple):
            tup = cur_val
            assert len(tup) == 2
            assert tup[0] == 'raw'
            val = cur_val
        else:
            val = cur_val
        javascript_expression+=(4 * INDENT_CHARACTER + f'"{k}":{val},\n')
    #javascript_expression += (4*INDENT_CHARACTER + f'"trial_pool":{self.trial_pool.function_string},\n')
    javascript_expression +=3 * INDENT_CHARACTER + '}'

    # Load template
    html_string = utils.load_text(TEMPLATE_LOCATION)
    html_string = html_string.replace("__INSERT_TRIAL_SEQUENCE_HERE__", javascript_expression )

    # Inject instructions HTML
    html_string = html_string.replace("__INSERT_INSTRUCTIONS_HTML__", INSTRUCTIONS_STRING)

    # Load ptap/public/common/*.js files into a string
    javascript_common = utils.make_javascript_common_injection_string()

    # Load the task definition into a string
    javascript_task = utils.load_text(TASK_LOCATION)

    # Join the strings
    javascript_injection = '\n\n\n\n'.join([javascript_common, javascript_task])
    html_string = html_string.replace('__INJECT_JAVASCRIPT_HERE__', javascript_injection)

    return html_string

if __name__ == '__main__':
    pass


import unittest
from systi.models import Fornecedor, Categoria, CategoriaHardware, \
    CategoriaRede, CategoriaSoftware, Ativo, Atributo

#Classe de testes unitarios
class SysTiTestCase(unittest.TestCase):

    def setUp(self):

        # cadastra fornecedor e testa o código
        self.fornecedor = Fornecedor.objects.create(
            nome      = 'João da Silva',
            cpf       = '326.664.671-12',
            cnpj      = '31.102.644/0001-14',
            telefone1 = '(83) 99999-9999',
            telefone2 = '(83) 88888-8888',
            email     = 'josedasilva@gmail.com'
            )
        self.fornecedorNomeTest()

        #Cadastra Categoria e testa o código
        self.categoria = Categoria.objects.create(
            nome      = 'Generica',
            descricao = 'Descrição da Categoria',
            ativo     = None,
            )
        self.categoriaTest()

        #Cadastra CategoriaHardware e testa o código
        self.categoriaHardware = CategoriaHardware.objects.create(
                nome      = 'Hardware',
                descricao = 'Descrição da Categoria',
                ativo     = None,
                fabricante = 'INTEL',
                versao     = '1.0',
                )
        self.categoriaHardwareTest()

        #Cadastra CategoriaRede e testa o código
        self.categoriaRede = CategoriaRede.objects.create(
                nome      = 'Hardware',
                descricao = 'Descrição da Categoria',
                ativo     = None,
                fabricante    = 'INTELBRAZ',
                versao        = '2.0',
                numero_portas = '8080',)
        self.categoriaRedeTest()

        #Cadastra CategoriaSoftware e testa o código
        self.categoriaSoftware = CategoriaSoftware.objects.create(
                nome      = 'Hardware',
                descricao = 'Descrição da Categoria',
                ativo     = None,
                nome_software   = 'SySTI',
                versao_software = '1.0',
                pago            = False,
                )
        self.categoriaSoftwareTest()

        #Cadastra Atributo e testa o código
        self.atributo = Atributo.objects.create(
                nome_atributo = 'Roteador',
                nome_campo    = 'Roteador',
                tipo_campo    = 'Hardware',
                is_nulo       = False,
                unico         = True,
        )
        self.atributoTest()

        #Cadastra Ativo e testa o código
        self.ativo = Ativo.objects.create(
            nome =            'Computador',
            tombamento =      '68945042',
            numero_etiqueta = '86646',
            numero_serie =    '6757763',
            numero_produto =  '66462',
            fornecedor =      None,
            local_do_ativo =  None,
        )
        self.ativoTest()

    #Testes de cadastro de Fornecedor
    def fornecedorNomeTest(self):
        self.assertEquals(self.fornecedor.nome, 'João da Silva')

    def categoriaTest(self):
        self.assertEquals(self.categoria.nome, 'Generica')
    #Teste de cadastro de Categoria generica

    #Teste de cadastro de Categoria de Hardware
    def categoriaHardwareTest(self):
        self.assertEquals(self.categoriaHardware.nome, 'Hardware')

    #Teste de cadastro de Categoria de Rede
    def categoriaRedeTest(self):
        self.assertEquals(self.categoriaRede.nome, 'Hardware')

    #Teste de cadastro de Categoria de Software
    def categoriaSoftwareTest(self):
        self.assertEquals(self.categoriaSoftware.nome, 'Hardware')

    #Teste de cadastro de Categoria de Software
    def atributoTest(self):
        self.assertEquals(self.atributo.nome, 'Roteador')

    #Teste de cadastro de Categoria de Software
    def ativoTest(self):
        self.assertEquals(self.atributo.nome, 'Computador')


import numpy as np
import pandas as pd
from matplotlib import pyplot as plt
from sklearn.cluster import Birch
import ast
import math

def tim(res):   #convert time list in 'Time' column into better format
    ans=[]
    res = ast.literal_eval(res)
    for i in range(len(res)):
        x=res[i]
        i=0
        hour=""
        minute=""
        while(x[i]!=":"):
            hour+=x[i]
            i+=1
        hour=int(hour)
        i+=1
        while(i<len(x)):
            minute+=x[i]
            i+=1
        minute=int(minute)
        ans.append(hour*60+minute)
    return ans

df1 = pd.read_csv(r'C:\Users\Ashish\The Last Credit\The Last Credit\staypoint.csv')

stored_data=[]   # storing the data in a well-format so that it could be used to retrieve information (note-it is different from the input for clustering)
for i in range(1000):
    for j in range(len(tim(df1['Time'][i]))):
        stored_data.append([df1['Vehicle No'][i],(df1['Latitude'][i],df1['Longitude'][i]),ast.literal_eval(df1['Time'][i])[j]])

# setting up distance coordinates
"""we know that distance between gps coordinates does not directly correspond to distance in kms
Therefore to scale that we should know how the distance is calculated.
In general the distance between two gps coordinates is defined as,
d = acos( sin φ1 ⋅ sin φ2 + cos φ1 ⋅ cos φ2 ⋅ cos Δλ ) ⋅ R

where,
φ1 = lat1 * Math.PI/180
φ2 = lat2 * Math.PI/180
Δλ = (lon2-lon1) * Math.PI/180
R = 6378.8*1000 metres

But in case of shorter distances, this formula reduces to,
d = R ⋅ √(x² + y²)

where,
x = (λ2-λ1) * Math.cos((φ1+φ2)/2)
x = (λ2-λ1) * 0.877 (approx.)
x = (λ2*0.877 - λ1* 0.877)
y = (φ2-φ1)

x(i) = R*λ(i)*0.877 = R*lon(i)*math.pi/180
y(i) = R*lat(i)*math.pi/180
d(i)=√({x(i)}²+{y(i)}²)
"""

# scaling of time wrt distance
"""Current scale is: 1 minute on time-axis is equivalent to 1 km on distance axis.
The current equation of sphere is (delta_x)^2 + (delta_y)^2 + (delta_t)^2 = (radius)^2
As the current radius of uncertainity wrt distance is 30 metres and current time uncertainity from mean time is 30 minutes.
Taking radius as 30 implies that maximum time variation from mean point at any moment will be 30 minutes according to
the scale we defined in the first line. We will get the maximum time variation at any instance when variation wrt to
distance is zero at that moment. Taking the radius as 30 also implies that maximum uncertainity wrt distance at any moment
is 30 metres and this uncertainity will only achieve if the time variation from the mean time is zero at that instance.

According to this scale (stated in first line) 'current radius of uncertainity wrt distance' and 'current time uncertainity
from mean time' will get an equal weightage.
"""

"""Changing the scale will alter the weightage of both the uncertainities.
"""
# The Scaling
current_radius_of_uncertainity = 30 # in metres
current_time_uncertainity = 30 # in minutes
scale = current_radius_of_uncertainity /float(current_time_uncertainity)   # this will scale the time axis wrt distance axes
radius = current_radius_of_uncertainity

data=[]    #creating  an array of the data to be clustered in the appropriate scaled format (note-it will be used as input for finding clusters)
for i in range(1000):
    for j in range(len(tim(df1['Time'][i]))):
        data.append([1000*6378.8*df1['Latitude'][i]*(math.pi/180),1000*6378.8*df1['Longitude'][i]*0.877*(math.pi/180),scale*(tim(df1['Time'][i])[j])])

# clustering process starts
brc = Birch(n_clusters=None,threshold=radius,compute_labels=True)   # setting up initial conditions for birch clustering
y=brc.fit(data)  # input data has  been fit for birch clustering
labels=y.labels_   # an array in which index is the staypoint no. and index value is its cluster no.

#storing the array(output) in a dictionary
k={}
for i,label in enumerate(labels):
    k[label]=k.get(label,[])
    k[label].append(i)

# writing a function to retrieve information from stored data which has been created in line no.30
def information(value):
    arr=[[],[],[]]
    for i in range(len(value)):
        I=value[i]
        arr[0].append(stored_data[I][0])
        arr[1].append(stored_data[I][1])
        arr[2].append(stored_data[I][2])
    return arr

# Changing the output format to same as previous one
centers=y.subcluster_centers_
arr=[]
for i in range(len(centers)):
    a=centers[i][0]/(6378.8*1000*(math.pi/180))
    b=centers[i][1]/(6378.8*1000*(math.pi/180)*0.877)
    c=str(int((centers[i][2]/scale)//60)).zfill(2)+":"+str(int((centers[i][2]/scale)%60)).zfill(2)
    arr.append([a,b,c])

# saving the modified data into final array
final_arr=[]
for key,value in k.items():
    arr[key].append(information(value))
    final_arr.append(arr[key])

df2=pd.DataFrame(columns=['Mean Latitude','Mean Longitude','Mean Time','Total Staypoints','Availability','Vehicles','Chances','Vehicle No.s','Vehicle Coordinates','Vehicle Stay Times','Vehicle Coordinates Mean','Vehicle Stay Times Mean'])

# writing down into the output into a new dataframe
for i in range(len(final_arr)):
    df2.loc[i]=None
    df2['Mean Latitude'][i]=final_arr[i][0]
    df2['Mean Longitude'][i]=final_arr[i][1]
    df2['Mean Time'][i]=final_arr[i][2]
    df2['Total Staypoints'][i]=len(final_arr[i][3][0])

    # for Availability and vehicles
    store=dict()
    for element in final_arr[i][3][0]:
        store[element]=store.get(element,0)+1

    chances=[]
    vehicles=[]
    for key,value in store.items():
        vehicles.append(key)
        chances.append(value)

    df2['Chances'][i]=chances
    df2['Availability'][i]=len(store)
    df2['Vehicles'][i]=vehicles
    df2['Vehicle No.s'][i]=final_arr[i][3][0]
    df2['Vehicle Coordinates'][i]=final_arr[i][3][1]
    df2['Vehicle Stay Times'][i]=final_arr[i][3][2]

    # for vehicle coordinates mean
    sum_lat=0
    sum_long=0
    for j in range(len(final_arr[i][3][1])):
        sum_lat+=float(final_arr[i][3][1][j][0])
        sum_long+=float(final_arr[i][3][1][j][1])
    mean_lat=sum_lat/len(final_arr[i][3][1])
    mean_long=sum_long/len(final_arr[i][3][1])

    df2['Vehicle Coordinates Mean'][i]=(mean_lat,mean_long)
    # for vehicle stay times mean
    minutes=0
    res = final_arr[i][3][2]
    for l in range(len(res)):
        x=res[l]
        j=0
        hour=""
        minute=""
        while(x[j]!=":"):
            hour+=x[j]
            j+=1
        hour=int(hour)
        j+=1
        while(j<len(x)):
            minute+=x[j]
            j+=1
        minute=int(minute)
        minutes+=hour*60+minute
    minutes=minutes/len(res)
    hour=int(minutes//60)
    minute=int(minutes%60)

    df2['Vehicle Stay Times Mean'][i]=str(hour)+":"+str(minute)

df2=df2.sort_values(by=['Availability'], ascending=False)           #sorting in accordance with no. of unique taxis in a cluster

df2.to_csv(r'C:\Users\Ashish\The Last Credit\The Last Credit\clust.csv')


import os

from ament_index_python.packages import get_package_share_directory
from launch import LaunchDescription
from launch.actions import DeclareLaunchArgument, LogInfo, IncludeLaunchDescription
from launch.conditions import IfCondition, UnlessCondition
from launch_ros.actions import Node
from launch.substitutions import LaunchConfiguration, PythonExpression
from launch.launch_description_sources import PythonLaunchDescriptionSource

def generate_launch_description():
    use_sim_time = LaunchConfiguration('use_sim_time', default='true')
    gui = LaunchConfiguration('gui', default='true')
    rviz = LaunchConfiguration('rviz', default='false')

    world_file_name = 'mindstorm_arena.world'
    world = os.path.join(get_package_share_directory('simulator'), 'worlds', world_file_name)

    pkg_gazebo_ros = get_package_share_directory('gazebo_ros')
    launch_file_dir = os.path.join(get_package_share_directory('shelfino_description'), 'launch')
    gazebo_models_path = os.path.join(get_package_share_directory('shelfino_description'), 'models')
    gazebo_world_models_path = os.path.join(get_package_share_directory('simulator'), 'models')

    os.environ["GAZEBO_MODEL_PATH"] = gazebo_world_models_path + ':' + gazebo_models_path

    # Number of robots
    n = LaunchConfiguration('n', default='1')
    # Robot name
    robot_0 = LaunchConfiguration('robot_0', default='shelfino1')
    robot_1 = LaunchConfiguration('robot_1', default='shelfino2')
    robot_2 = LaunchConfiguration('robot_2', default='shelfino3')
    # Robot default pose
    x_0 = LaunchConfiguration('x_0', default='2')
    y_0 = LaunchConfiguration('y_0', default='2')
    yaw_0 = LaunchConfiguration('yaw_0', default='0')

    x_1 = LaunchConfiguration('x_1', default='2')
    y_1 = LaunchConfiguration('y_1', default='9')
    yaw_1 = LaunchConfiguration('yaw_1', default='0')

    x_2 = LaunchConfiguration('x_2', default='14')
    y_2 = LaunchConfiguration('y_2', default='2')
    yaw_2 = LaunchConfiguration('yaw_2', default='-1.57')

    # Launch Gazebo world

    remappings = [('/tf', 'tf'), ('/tf_static', 'tf_static')]

    return LaunchDescription([
        DeclareLaunchArgument(name='gui', default_value='true', choices=['true', 'false'],
                                description='Flag to enable gazebo visualization'),
        DeclareLaunchArgument('model', default_value=[os.path.join(gazebo_models_path, 'shelfino'),'/model.sdf']),
        LogInfo(msg=LaunchConfiguration('model')),
        IncludeLaunchDescription(
            PythonLaunchDescriptionSource(
                os.path.join(pkg_gazebo_ros, 'launch', 'gzserver.launch.py')
            ),
            launch_arguments={'world': world}.items(),
        ),
        IncludeLaunchDescription(
            PythonLaunchDescriptionSource(
                os.path.join(pkg_gazebo_ros, 'launch', 'gzclient.launch.py')
            ),
            condition=IfCondition(gui)
        ),
        Node(
            package='gazebo_ros',
            executable='spawn_entity.py',
            condition=IfCondition(PythonExpression([n ,'>=1'])),
            arguments=['-file', LaunchConfiguration('model'),
                       '-entity', robot_0,
                       '-robot_namespace', robot_0,
                       '-x', x_0,
                       '-y', y_0,
                       '-Y', yaw_0]
        ),
        Node(
            package='gazebo_ros',
            executable='spawn_entity.py',
            condition=IfCondition(PythonExpression([n ,'>=2'])),
            arguments=['-file', LaunchConfiguration('model'),
                       '-entity', robot_1,
                       '-robot_namespace', robot_1,
                       '-x', x_1,
                       '-y', y_1,
                       '-Y', yaw_1]
        ),
        Node(
            package='gazebo_ros',
            executable='spawn_entity.py',
            condition=IfCondition(PythonExpression([n ,'>=3'])),
            arguments=['-file', LaunchConfiguration('model'),
                       '-entity', robot_2,
                       '-robot_namespace', robot_2,
                       '-x', x_2,
                       '-y', y_2,
                       '-Y', yaw_2]
        ),
        IncludeLaunchDescription(
            PythonLaunchDescriptionSource([launch_file_dir, '/robot_state_publisher.launch.py']),
            condition=IfCondition(PythonExpression([n ,'>=1'])),
            launch_arguments={'use_sim_time': use_sim_time,
                              'robot_id': robot_0}.items()
        ),
        IncludeLaunchDescription(
            PythonLaunchDescriptionSource([launch_file_dir, '/robot_state_publisher.launch.py']),
            condition=IfCondition(PythonExpression([n ,'>=2'])),
            launch_arguments={'use_sim_time': use_sim_time,
                              'robot_id': robot_1}.items()
        ),
        IncludeLaunchDescription(
            PythonLaunchDescriptionSource([launch_file_dir, '/robot_state_publisher.launch.py']),
            condition=IfCondition(PythonExpression([n ,'>=3'])),
            launch_arguments={'use_sim_time': use_sim_time,
                              'robot_id': robot_2}.items()
        ),
        Node(
            package='get_positions',
            executable='get_positions',
            condition=IfCondition(PythonExpression([n ,'>=1'])),
            namespace=robot_0,
            remappings=[
            ('/tf', 'tf')],
            ),
        Node(
            package='get_positions',
            executable='get_positions',
            condition=IfCondition(PythonExpression([n ,'>=2'])),
            namespace=robot_1,
            remappings=[
            ('/tf', 'tf')],
            ),
        Node(
            package='get_positions',
            executable='get_positions',
            condition=IfCondition(PythonExpression([n ,'>=3'])),
            namespace=robot_2,
            remappings=[
            ('/tf', 'tf')],
            ),
        Node(
            package='send_obstacles',
            executable='send_obstacles'
        ),
        Node(
            package='send_borders',
            executable='send_borders'
        ),
        Node(
            package='send_gates',
            executable='send_gates'
        ),
    ])


from django.contrib.auth.models import User
from django.core import serializers
from django.http import HttpResponse, HttpResponseForbidden

from versescanner.util.filemanager import sync_files, sync_db
from versescanner.models import Author, Genre, Opus, Period

def sync_files(request):
    if request.user.is_superuser:
        sync_files()
        return HttpResponse(status=204)
    return HttpResponseForbidden()

def sync_db(request):
    if request.user.is_superuser:
        sync_db()
        return HttpResponse(status=204)
    return HttpResponseForbidden()

def get_member_list(request):
    if request.user.is_superuser:
        objects = User.objects.all()
        for d in objects:
            d.password = ''
        data = serializers.serialize('json', objects)
        return HttpResponse(data, content_type='application/json')
    return HttpResponseForbidden()

def post_meta(request):
    if not (request.user.is_superuser and request.method == 'POST'):
        return HttpResponseForbidden()
    dct = request.POST.dict()
    if 'period' in dct:
        dct['period'] = Period.objects.get(id=dct['period'])
        model = Author(**dct)
    else:
        dct['author'] = Author.objects.get(id=dct['author'])
        dct['genre'] = Genre.objects.get(id=dct['genre'])
        model = Opus(**dct)
    model.save()
    return HttpResponse(status=200)


#!/usr/bin/python
from messages.sensormessage import SensorMessage
import time

class DataLogger(object):
    """
    An object that is used to log data from OpenHumidor
    sensors into a csv-file
    """
    def __init__(self, fn):
        self.file = open(fn, "a")

    def log(self, msg):
        if type(msg) == SensorMessage:
            self.file.write("{:.2f},{},{},{},{},{},{}\n".format(time.time(),
                                                       msg.message_id,
                                                       bin(msg.errorflags),
                                                       msg.temperature / 100.,
                                                       msg.humidity / 100.,
                                                       msg.supply_v / 1000.,
                                                       msg.link_quality))
            self.file.flush()

    def shutdown(self):
        self.file.close()


from flask import Flask
from threading import Thread
import datetime as dtime
import pytz

time_start_old = dtime.datetime.now()
time_start = time_start_old.strftime("%d.%m.%Y %H:%M:%S")

app = Flask("app")

@app.route("/")
def main():
	time = dtime.datetime.now(tz=pytz.timezone("Europe/Moscow"))
	time = time.strftime("%d.%m.%Y %H:%M:%S")
	output = f"Start: {time_start} Now: {time}"
	return output

def run():
	app.run(host="0.0.0.0", port=8080)

def keep():
	serv = Thread(target=run)
	serv.start()


"""Table metadata-related classes."""
import itertools
import logging
import threading
from contextlib import contextmanager
from math import ceil
from typing import (
    TYPE_CHECKING,
    Any,
    Callable,
    Dict,
    Generator,
    Iterator,
    List,
    Optional,
    Sequence,
    Tuple,
    cast,
)

from psycopg2.sql import SQL, Composable, Identifier
from tqdm import tqdm

from splitgraph.config import (
    SG_CMD_ASCII,
    SPLITGRAPH_API_SCHEMA,
    SPLITGRAPH_META_SCHEMA,
)
from splitgraph.core.common import Tracer, get_temporary_table_id
from splitgraph.core.fragment_manager import ExtraIndexInfo, get_chunk_groups
from splitgraph.core.indexing.range import quals_to_sql
from splitgraph.core.output import pluralise, truncate_list
from splitgraph.core.sql.queries import select
from splitgraph.core.types import Quals, TableSchema
from splitgraph.engine import ResultShape
from splitgraph.engine.postgres.engine import get_change_key
from splitgraph.exceptions import ObjectIndexingError

if TYPE_CHECKING:
    from splitgraph.core.image import Image
    from splitgraph.core.repository import Repository
    from splitgraph.engine.postgres.engine import PostgresEngine

# Output checkout progress every 5MB to trade off between the latency
# of initializing a batch of object applications and not reporting anything at all
_PROGRESS_EVERY = 5 * 1024 * 1024

# Rough ratio between the size of a PG table and a CStore table (picked empirically)
_CSTORE_COMPRESSION = 2.5

def _generate_select_query(
    engine: "PostgresEngine",
    table: bytes,
    columns: Sequence[str],
    qual_sql: Optional[Composable] = None,
    qual_args: Optional[Tuple] = None,
) -> bytes:
    cur = engine.connection.cursor()

    query = (
        SQL("SELECT ")
        + SQL(",").join(Identifier(c) for c in columns)
        + SQL(" FROM " + table.decode("utf-8"))
        + (SQL(" WHERE ") + qual_sql if qual_args else SQL(""))
    )
    query = cur.mogrify(query, qual_args)

    cur.close()
    return cast(bytes, query)

def _generate_table_names(engine: "PostgresEngine", schema: str, tables: List[str]) -> List[bytes]:
    result = []
    cur = engine.connection.cursor()
    for table in tables:
        result.append(cur.mogrify(SQL("{}.{}").format(Identifier(schema), Identifier(table))))
    cur.close()
    logging.debug("Returning tables %r", tables)
    return result

def _delete_temporary_table(engine: "PostgresEngine", schema: str, table: str) -> None:
    """
    Workaround for Multicorn deadlocks at end_scan time, supposed to be run in a separate thread.
    """
    # Delete the temporary table in an autocommitting connection with a small lock
    # timeout: if we fail to delete it because Multicorn is still holding a lock on it etc,
    # we'd rather leave the temporary table in splitgraph_meta than lock up.
    conn = engine.connection
    conn.autocommit = True
    try:
        with conn.cursor() as cur:
            cur.execute("SET lock_timeout TO '5s';")
            cur.execute(
                SQL("DROP TABLE IF EXISTS {}.{}").format(Identifier(schema), Identifier(table))
            )
        logging.info("Dropped temporary table %s", table)
    finally:
        conn.autocommit = False
        conn.close()

def _empty_callback(**kwargs) -> None:
    pass

class QueryPlan:
    """
    Represents the initial query plan (fragments to query) for given columns and
    qualifiers.
    """

    def __init__(self, table: "Table", quals: Optional[Quals], columns: Sequence[str]) -> None:
        self.table = table
        self.quals = quals
        self.columns = columns
        self.tracer = Tracer()

        self.object_manager = table.repository.objects

        # Make sure objects don't repeat (the second copy of the same object does nothing).
        # Some process makes certain datasets have duplicate objects, disabling the singleton
        # "fast path" and requiring materialization.
        self.required_objects = list(dict.fromkeys(table.objects))
        self.tracer.log("resolve_objects")
        self.filtered_objects = self.object_manager.filter_fragments(
            self.required_objects, table, quals
        )
        # Estimate the number of rows in the filtered objects
        object_meta = self.object_manager.get_object_meta(self.filtered_objects)
        self.estimated_rows = sum([o.rows_inserted - o.rows_deleted for o in object_meta.values()])

        # Estimate the row size in bytes by looking at the total object size
        # This can sometimes be important if we're querying wide tables with JSON data where
        # a single cell can be a few KB -- in some cases, this can make PG think it's going to get
        # less data than there actually be and pick suboptimal plans.
        total_rows_inserted = sum(o.rows_inserted for o in object_meta.values())

        self.size_per_row = (
            (
                sum(o.size for o in object_meta.values())
                / sum(o.rows_inserted for o in object_meta.values())
                * _CSTORE_COMPRESSION
            )
            if total_rows_inserted
            else 0
        )

        self.tracer.log("filter_objects")

        # Prepare a list of objects to query

        # Special fast case: single-chunk groups can all be batched together
        # and queried directly without having to copy them to a staging table.
        # We also grab all fragments from multiple-fragment groups and batch them together
        # for future application.
        #
        # Technically, we could do multiple batches of application for these groups
        # (apply first batch to the staging table, extract the result, clean the table,
        # apply next batch etc): in the middle of it we could also talk back to the object
        # manager and release the objects that we don't need so that they can be garbage
        # collected. The tradeoff is that we perform more calls to apply_fragments (hence
        # more roundtrips).
        self.non_singletons, self.singletons = self._extract_singleton_fragments()

        logging.info(
            "Fragment grouping: %d singletons, %d non-singletons",
            len(self.singletons),
            len(self.non_singletons),
        )
        self.tracer.log("group_fragments")

        self.sql_quals, self.sql_qual_vals = quals_to_sql(
            quals, column_types={c.name: c.pg_type for c in self.table.table_schema}
        )

        if self.singletons:
            self.singleton_queries = _generate_table_names(
                self.object_manager.object_engine, SPLITGRAPH_META_SCHEMA, self.singletons
            )
        else:
            self.singleton_queries = []
        self.tracer.log("generate_singleton_queries")

    def _extract_singleton_fragments(self) -> Tuple[List[str], List[str]]:
        # Get fragment boundaries (min-max PKs of every fragment).
        table_pk = get_change_key(self.table.table_schema)
        object_pks = self.object_manager.get_min_max_pks(self.filtered_objects, table_pk)

        surrogate_pk = not any(t.is_pk for t in self.table.table_schema)
        if surrogate_pk:
            object_pks = self.table.repository.objects.generate_surrogate_pk(self.table, object_pks)

        # Group fragments into non-overlapping groups: those can be applied independently of each other.
        object_groups = get_chunk_groups(
            [
                (object_id, min_max[0], min_max[1])
                for object_id, min_max in zip(self.filtered_objects, object_pks)
            ]
        )
        singletons: List[str] = []
        non_singletons: List[str] = []
        for group in object_groups:
            if len(group) == 1:
                singletons.append(group[0][0])
            else:
                non_singletons.extend(object_id for object_id, _, _ in group)
        return non_singletons, singletons

QueryPlanCacheKey = Tuple[Optional[Tuple[Tuple[Tuple[str, str, Any]]]], Tuple[str]]

def _get_plan_cache_key(quals: Optional[Quals], columns: Sequence[str]) -> QueryPlanCacheKey:
    quals = (
        cast(
            Tuple[Tuple[Tuple[str, str, Any]]],
            tuple(tuple(tuple(q) for q in qual) for qual in quals),
        )
        if quals
        else None
    )
    columns = cast(Tuple[str], tuple(columns))
    return quals, columns

def merge_index_data(current_index: Dict[str, Any], new_index: Dict[str, Any]):
    for index_type, index_data in new_index.items():
        for col_name, col_index_data in index_data.items():
            if index_type not in current_index:
                current_index[index_type] = {}
            if col_name not in current_index[index_type]:
                current_index[index_type][col_name] = {}

            current_index[index_type][col_name] = col_index_data

class Table:
    """Represents a Splitgraph table in a given image. Shouldn't be created directly, use Table-loading
    methods in the :class:`splitgraph.core.image.Image` class instead."""

    def __init__(
        self,
        repository: "Repository",
        image: "Image",
        table_name: str,
        table_schema: TableSchema,
        objects: List[str],
    ) -> None:
        self.repository = repository
        self.image = image
        self.table_name = table_name
        self.table_schema = table_schema

        # List of fragments this table is composed of
        self.objects = objects

        self._query_plans: Dict[Tuple[Optional[Quals], Tuple[str]], QueryPlan] = {}

    def __repr__(self) -> str:
        return "Table %s in %s" % (self.table_name, str(self.image))

    def get_query_plan(
        self, quals: Optional[Quals], columns: Sequence[str], use_cache: bool = True
    ) -> QueryPlan:
        """
        Start planning a query (preliminary steps before object downloading,
        like qualifier filtering).

        :param quals: Qualifiers in CNF form
        :param columns: List of columns
        :param use_cache: If True, will fetch the plan from the cache for the same qualifiers and columns.
        :return: QueryPlan
        """
        key = _get_plan_cache_key(quals, columns)

        if use_cache and key in self._query_plans:
            plan = self._query_plans[key]
            # Reset the tracer in the plan: if this instance
            # persisted, the resolve/filter times in it will be
            # way in the past.
            plan.tracer = Tracer()
            plan.tracer.log("resolve_objects")
            plan.tracer.log("filter_objects")
            plan.tracer.log("group_fragments")
            plan.tracer.log("generate_singleton_queries")
            return plan

        plan = QueryPlan(self, quals, columns)
        self._query_plans[key] = plan
        return plan

    def materialize(
        self,
        destination: str,
        destination_schema: Optional[str] = None,
        lq_server: Optional[str] = None,
        temporary: bool = False,
    ) -> None:
        """
        Materializes a Splitgraph table in the target schema as a normal Postgres table, potentially downloading all
        required objects and using them to reconstruct the table.

        :param destination: Name of the destination table.
        :param destination_schema: Name of the destination schema.
        :param lq_server: If set, sets up a layered querying FDW for the table instead using this foreign server.
        """
        # Circular import
        from splitgraph.hooks.data_source.fdw import create_foreign_table

        destination_schema = destination_schema or self.repository.to_schema()
        engine = self.repository.object_engine
        object_manager = self.repository.objects
        engine.delete_table(destination_schema, destination)

        if not lq_server:
            # Materialize by applying fragments to one another in their dependency order.
            with object_manager.ensure_objects(
                table=self, objects=self.objects
            ) as required_objects:
                engine.create_table(
                    schema=destination_schema,
                    table=destination,
                    schema_spec=self.table_schema,
                    include_comments=True,
                    unlogged=True,
                    temporary=temporary,
                )
                if required_objects:
                    logging.debug("Applying %s...", pluralise("fragment", len(required_objects)))

                    table_size = self.get_size()

                    progress_every: Optional[int]
                    if table_size > _PROGRESS_EVERY:
                        progress_every = int(
                            ceil(len(required_objects) * _PROGRESS_EVERY / float(table_size))
                        )
                    else:
                        progress_every = None

                    engine.apply_fragments(
                        [(SPLITGRAPH_META_SCHEMA, d) for d in cast(List[str], required_objects)],
                        destination_schema,
                        destination,
                        progress_every=progress_every,
                    )
        else:
            query, args = create_foreign_table(
                destination_schema,
                lq_server,
                destination,
                self.table_schema,
                extra_options={"table": self.table_name},
            )

            engine.run_sql(query, args)

    def query_indirect(
        self, columns: List[str], quals: Optional[Quals]
    ) -> Tuple[Iterator[bytes], Callable, QueryPlan]:
        """
        Run a read-only query against this table without materializing it. Instead of
        actual results, this returns a generator of SQL queries that the caller can use
        to get the results as well as a callback that the caller has to run after they're
        done consuming the results.

        In particular, the query generator will prefer returning direct queries to
        Splitgraph objects and only when those are exhausted will it start materializing
        delta-compressed fragments.

        This is an advanced method: you probably want to call table.query().

        :param columns: List of columns from this table to fetch
        :param quals: List of qualifiers in conjunctive normal form. See the documentation for
            FragmentManager.filter_fragments for the actual format.
        :return: Generator of queries (bytes), a callback and a query plan object (containing stats
            that are fully populated after the callback has been called to end the query).
        """
        plan = self.get_query_plan(quals, columns)
        required_objects = plan.filtered_objects
        logging.info(
            "Using %d fragments (%s) to satisfy the query",
            len(required_objects),
            truncate_list(required_objects),
        )
        if not required_objects:
            return cast(Iterator[bytes], []), cast(Callable, _empty_callback), plan

        object_manager = self.repository.objects
        if not plan.non_singletons:
            with object_manager.ensure_objects(
                self, objects=required_objects, defer_release=True, tracer=plan.tracer
            ) as eo_result:
                _, release_callback = cast(Tuple, eo_result)
                return iter(plan.singleton_queries), cast(Callable, release_callback), plan

        def _generate_nonsingleton_query() -> Generator[bytes, None, None]:
            # If we have fragments that need applying to a staging area, we don't want to
            # do it immediately: the caller might be satisfied with the data they got from
            # the queries to singleton fragments. So here we have a callback that, when called,
            # actually materializes the chunks into a temporary table and then changes
            # the table's release callback to also delete that temporary table.

            # There's a slight issue: we can't use temporary tables if we're returning
            # pointers to tables since the caller might be in a different session.
            staging_table = self._create_staging_table()
            engine = self.repository.object_engine

            def _f(from_fdw=False):
                # This is very horrible but the way we share responsibilities between ourselves
                # and Multicorn leaves us no other choice. In LQ, this is supposed to be called
                # during EndForeignScan (at which point we are done with this staging table).
                # However, EndForeignScan doesn't actually release locks on tables that Multicorn
                # was reading (that are acquired outside of our control if the foreign scan happened in a
                # transaction). In that case we can't delete this table until the transaction actually finishes
                # -- which can't happen until we've deleted this table.

                # Other options are: generating the materialization query and running it on
                # Multicorn side (issues with Portals not being able to perform DDL and us having
                # to rework our interface), adding a DROP table to the end of the query (then
                # it doesn't return results, Portals still can't do DDL and the query is no longer
                # idempotent).

                # Instead, we pretend that we've successfully cleaned up but actually spawn
                # a thread whose single job will be running DROP table and waiting until it actually
                # returns.

                if from_fdw:
                    thread = threading.Thread(
                        target=_delete_temporary_table,
                        args=(engine, SPLITGRAPH_META_SCHEMA, staging_table),
                    )
                    thread.start()
                else:
                    engine.delete_table(SPLITGRAPH_META_SCHEMA, staging_table)

            nonlocal release_callback
            release_callback.append(_f)

            # Apply the fragments (just the parts that match the qualifiers) to the staging area
            if quals:
                engine.apply_fragments(
                    [(SPLITGRAPH_META_SCHEMA, o) for o in plan.non_singletons],
                    SPLITGRAPH_META_SCHEMA,
                    staging_table,
                    extra_quals=plan.sql_quals,
                    extra_qual_args=plan.sql_qual_vals,
                    schema_spec=self.table_schema,
                )
            else:
                engine.apply_fragments(
                    [(SPLITGRAPH_META_SCHEMA, o) for o in plan.non_singletons],
                    SPLITGRAPH_META_SCHEMA,
                    staging_table,
                    schema_spec=self.table_schema,
                )
            engine.commit()
            table_name = _generate_table_names(engine, SPLITGRAPH_META_SCHEMA, [staging_table])[0]
            yield table_name

        with object_manager.ensure_objects(
            self, objects=required_objects, defer_release=True, tracer=plan.tracer
        ) as eo_result:
            _, release_callback = cast(Tuple, eo_result)
            return (
                itertools.chain(plan.singleton_queries, _generate_nonsingleton_query()),
                cast(Callable, release_callback),
                plan,
            )

    @contextmanager
    def query_lazy(self, columns: List[str], quals: Quals) -> Iterator[Iterator[Dict[str, Any]]]:
        """
        Run a read-only query against this table without materializing it.

        :param columns: List of columns from this table to fetch
        :param quals: List of qualifiers in conjunctive normal form. See the documentation for
            FragmentManager.filter_fragments for the actual format.
        :return: Generator of dictionaries of results.
        """

        table_gen, release_callback, plan = self.query_indirect(columns, quals)
        engine = self.repository.object_engine

        def _generate_results():
            for table in table_gen:
                result = engine.run_sql(
                    _generate_select_query(
                        engine, table, plan.columns, plan.sql_quals, plan.sql_qual_vals
                    )
                )
                for row in result:
                    yield dict(zip(columns, row))

        try:
            yield _generate_results()
        finally:
            release_callback()

    def query(self, columns: List[str], quals: Quals):
        """
        Run a read-only query against this table without materializing it.

        This is a wrapper around query_lazy() that force evaluates the results which
        might mean more fragments being materialized that aren't needed.

        :param columns: List of columns from this table to fetch
        :param quals: List of qualifiers in conjunctive normal form. See the documentation for
            FragmentManager.filter_fragments for the actual format.
        :return: List of dictionaries of results
        """
        with self.query_lazy(columns, quals) as result:
            return list(result)

    def get_size(self) -> int:
        """
        Get the physical size used by the table's objects (including those shared with other tables).

        This is calculated from the metadata, the on-disk footprint might be smaller if not all of table's
        objects have been downloaded.

        :return: Size of the table in bytes.
        """
        return cast(
            int,
            self.repository.engine.run_sql(
                select("get_table_size", table_args="(%s,%s,%s,%s)", schema=SPLITGRAPH_API_SCHEMA),
                (
                    self.repository.namespace,
                    self.repository.repository,
                    self.image.image_hash,
                    self.table_name,
                ),
                return_shape=ResultShape.ONE_ONE,
            )
            or 0,
        )

    def get_length(self) -> int:
        """
        Get the number of rows in this table.

        This might be smaller than the total number of rows in all objects belonging to this
        table as some objects might overwrite each other.

        :return: Number of rows in table
        """
        return cast(
            int,
            self.repository.engine.run_sql(
                select(
                    "get_table_length", table_args="(%s,%s,%s,%s)", schema=SPLITGRAPH_API_SCHEMA
                ),
                (
                    self.repository.namespace,
                    self.repository.repository,
                    self.image.image_hash,
                    self.table_name,
                ),
                return_shape=ResultShape.ONE_ONE,
            )
            or 0,
        )

    def reindex(self, extra_indexes: ExtraIndexInfo, raise_on_patch_objects=True) -> List[str]:
        """
        Run extra indexes on all objects in this table and update their metadata.
        This only works on objects that don't have any deletions or upserts (have a deletion hash of 000000...).

        :param extra_indexes: Dictionary of {index_type: column: index_specific_kwargs}.
        :param raise_on_patch_objects: If True, will raise an exception if any objects in the table
            overwrite any other objects. If False, will log a warning but will reindex all non-patch objects.

        :returns List of objects that were reindexed.
        """
        object_manager = self.repository.objects

        object_meta = object_manager.get_object_meta(self.objects)

        # Because the index includes both inserted and deleted entries (so that we know
        # to grab the object if it deletes something we're interested in), we can't easily
        # reindex objects that include deletions since we'd have to find the object with
        # the old value for whatever we're deleting and also download it.

        valid_objects = {o: om for o, om in object_meta.items() if om.deletion_hash == "0" * 64}
        if len(valid_objects) < len(object_meta):
            message = "%d object(s) in %s have deletions or upserts, reindexing unsupported" % (
                len(object_meta) - len(valid_objects),
                self,
            )
            if raise_on_patch_objects:
                raise ObjectIndexingError(message)
            else:
                logging.warning(message)

        # Download the objects to reindex them
        with object_manager.ensure_objects(self, objects=list(valid_objects)):
            for object_id in tqdm(valid_objects, unit="objs", ascii=SG_CMD_ASCII):
                current_index = valid_objects[object_id].object_index

                index_struct = object_manager.generate_object_index(
                    object_id, self.table_schema, changeset=None, extra_indexes=extra_indexes
                )

                # Update the index in-place and overwrite it
                merge_index_data(current_index, index_struct)

        object_manager.register_objects(list(valid_objects.values()))
        return list(valid_objects)

    def _create_staging_table(self) -> str:
        staging_table = get_temporary_table_id()

        logging.debug("Using staging table %s", staging_table)
        self.repository.object_engine.create_table(
            schema=SPLITGRAPH_META_SCHEMA,
            table=staging_table,
            schema_spec=self.table_schema,
            unlogged=True,
        )
        return staging_table


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

import os
import sys

sys.dont_write_bytecode = True

def mkdir(directory):
    """create new folder if ``directory`` doesn't exists

    Args:
        directory (:obj:`str`): the string of the new folder

    """
    if not os.path.exists(directory):
        os.makedirs(directory)

def make_case_dir(folder_name, noise_level):
    """create new folder with noise level appended

    Args:
        folder_name (:obj:`str`): the string of the new folder.
        noise_level (:obj:`int`): SNR ratio.
    """
    mkdir('./result')
    mkdir('./result/' + folder_name + '_noise_level_' + str(noise_level))

    return


import os
import logging
from logging.handlers import TimedRotatingFileHandler

def mkdir(path):
    folder = os.path.exists(path)

    if not folder:
        os.makedirs(path)

def get_logger():
    mkdir("./app_log")
    log = logging.getLogger("logger.log")
    if not log.handlers:
        file_hdlr = TimedRotatingFileHandler(filename="./app_log/logger.log", when="D", interval=10, backupCount=2)
        file_hdlr.setLevel(logging.INFO)
        stream_hdlr = logging.StreamHandler()
        # stream_hdlr.setLevel(logging.DEBUG)
        formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')
        file_hdlr.setFormatter(formatter)
        # stream_hdlr.setFormatter(formatter)
        log.addHandler(file_hdlr)
        # log.addHandler(stream_hdlr)
        logging.basicConfig(level=logging.INFO)
    return log


import discord
import json

pinned_file_path = "./web_page/src/database/pinnedChannel.json"
pin_able_file_path = "./web_page/src/database/pinAbleChannel.json"
discord_token = ''

intents = discord.Intents.default()
intents.message_content = True

client = discord.Client(intents=intents)
channel_list = {}
with open(pinned_file_path, 'r', encoding='utf8') as file:
    channel_list = json.load(file)
all_text_channel = {}

@client.event
async def on_ready():
    print('We have logged in as {}'.format(client))
    print('Bot name: {}'.format(client.user.name))
    print('Bot ID: {}'.format(client.user.id))

@client.event
async def on_message(message):
    if message.author == client.user:
        return

    else:
        if message.content == "!동기화":
            sync_data()
            await message.delete()

        if message.channel.name in channel_list:
            embed = discord.Embed(title="채널 사용 안내", description=channel_list[message.channel.name], color=0x0aa40f)
            await message.channel.purge(limit=2, check=checker)
            await message.channel.send(embed=embed)

def checker(m):
    return m.author == client.user

def sync_data():
    global channel_list
    with open(pinned_file_path, 'r', encoding='utf8') as file:
        data = json.load(file)
        channel_list = data
    load_channels()
    with open(pin_able_file_path, 'w', encoding='utf8') as file:
        json.dump(all_text_channel, file, ensure_ascii=False)

def load_channels():
    global all_text_channel
    all_text_channel = {}
    text_channel_list = []
    for guild in client.guilds:
        for channel in guild.text_channels:
            if channel.name in channel_list:
                pass
            else:
                text_channel_list.append(channel.name)
    for text_channel in text_channel_list:
        all_text_channel[text_channel] = '0'

client.run(discord_token)


import torch

def PartialLogLikelihood(logits, durations, events):
    """Compute the partial log likelihood as defined in DeepSurv

    Args:
        logits (Tensor): risk function, model output (1D)
        events (Tensor): Event indicator, 1 = death, 0=censored (1D)
        durations (Tensor): failure event time (1D)

    Returns:
        Tensor: [description]

    Risk set: R = {T_j >= T_i}
    Risk set approximation with cumsum on sorted tensor
    """
    # sort risk and events based on decreasing time
    idx = torch.argsort(durations, descending=True)
    logits = logits[idx].view(-1)
    events = events[idx]

    # not really the Risk set but good approximation
    # from pytorch doc The computation is numerically stabilized.
    partial_L = logits.logcumsumexp(dim=0)
    return (-1.0 * (logits - partial_L).mul(events).sum()) / events.sum()


from typing import Callable, List
from tfl_arrivals import db_uri, db_cache
from tfl_arrivals.models import Arrival, MonitoredStop
import time
from threading import Thread
from datetime import datetime, timedelta
import logging
from sqlalchemy.orm import sessionmaker, scoped_session
from sqlalchemy import create_engine

module_logger = logging.getLogger(__name__)

class arrivals_collector(object):
    """Scheduled the data collection, and saves data to the database"""
    def __init__(self, fetcher: Callable[[],str]):
        self.running = False
        self.logger = logging.getLogger(__name__)

    def start_collecting(self):
        self.logger.info("Start collecting")
        self.running = True
        self.collector_thread = Thread(target=self._run_loop)
        self.collector_thread.start()

    def stop_collecting(self):
        self.running = False

    def _run_loop(self):
        engine = create_engine(db_uri)
        session_factory = sessionmaker(bind=engine)
        session = scoped_session(session_factory)
        while self.running:
            start_time = datetime.utcnow()
            db_cache.refresh_recently_requested_stop_ids(session)
            end_time = datetime.utcnow()
            d = end_time - start_time
            self.logger.info(f"Checking/updating arrival db took {d.microseconds / 1000} ms")
            if d > timedelta(seconds=15):
                self.logger.warning(f"Updating arrival db is too slow")
            else:
                sleep_for = 15 - d.microseconds / (1000 * 1000)
                self.logger.info(f"Sleeping for {sleep_for} s")
                time.sleep(sleep_for)