from __future__ import print_function, unicode_literals, absolute_import

from datetime import datetime
import json
import os
import re
import subprocess
import sys

from workflow import Workflow, web, ICON_WARNING

# How many posts to retrieve
POST_COUNT = 50

# How many subreddits to retrieve
SUBREDDIT_COUNT = 25

# How long to cache results for
CACHE_MAX_AGE = 180

# GitHub update settings
UPDATE_SETTINGS = {'github_slug': 'deanishe/alfred-reddit'}

HELP_URL = 'https://github.com/deanishe/alfred-reddit'

ICON_REDDIT = os.path.join(os.path.dirname(__file__), 'icon.png')
ICON_UPDATE = os.path.join(os.path.dirname(__file__), 'update-available.png')

POST_URL = 'http://www.reddit.com/r/{name}/hot.json'

SEARCH_URL = 'http://www.reddit.com/subreddits/search.json'

POPULAR_URL = 'http://www.reddit.com/subreddits/popular.json'

USER_AGENT = 'Alfred-Reddit/{version} ({url})'

log = None

def cache_key(name):
    """Make filesystem-friendly cache key"""
    key = name.lower()
    key = re.sub(r'[^a-z0-9-_\.]', '-', key)
    key = re.sub(r'-+', '-', key)
    log.debug('Cache key : {!r} -> {!r}'.format(name, key))
    return key

def relative_time(timestamp):
    """Return human-readable, relative time"""
    now = datetime.utcnow()
    postdate = datetime.utcfromtimestamp(timestamp)
    delta = now - postdate
    seconds = delta.seconds
    days, rem = divmod(seconds, 86400)
    hours, rem = divmod(rem, 3600)
    minutes, seconds = divmod(rem, 60)
    # log.debug('Post is {!r} old'.format(delta))
    if days:
        return '{} day{} ago'.format(days, 's'[days == 1:])
    if hours:
        return '{} hr{} ago'.format(hours, 's'[hours == 1:])
    if minutes:
        return '{} min{} ago'.format(minutes, 's'[minutes == 1:])
    if seconds:
        return '{} sec{} ago'.format(seconds, 's'[seconds == 1:])
    else:
        return '1 sec ago'

def handle_post(api_dict):
    """Strip down API dict"""
    data = api_dict.get('data', {})
    post = {}
    for key in ('title', 'url', 'author'):
        post[key] = data[key]
    post['timestamp'] = data['created_utc']
    post['reltime'] = relative_time(data['created_utc'])
    post['permalink'] = 'http://www.reddit.com{}'.format(data['permalink'])
    return post

def handle_subreddit(api_dict):
    """Strip down API dict"""
    data = api_dict.get('data', {})
    sr = {}
    sr['name'] = data['display_name']
    sr['title'] = data['title']
    sr['type'] = data['subreddit_type']

    return sr

def popular_subreddits(limit=SUBREDDIT_COUNT):
    """Return list of popular subreddits"""
    log.debug('Fetching list of popular subreddits ...')

    headers = {'user-agent': USER_AGENT.format(version=wf.version,
                                               url=wf.help_url)}

    params = {'limit': limit}

    r = web.get(POPULAR_URL, params, headers=headers)

    log.debug('[{}] {}'.format(r.status_code, r.url))

    r.raise_for_status()

    subreddits = r.json()['data']['children']
    # log.debug(pformat(subreddits))
    subreddits = [handle_subreddit(d) for d in subreddits]
    subreddits = [d for d in subreddits if d['type'] != 'private']

    for sr in subreddits:
        log.debug(sr)

    return subreddits

def search_subreddits(query, limit=SUBREDDIT_COUNT):
    """Return list of subreddits matching `query`"""
    log.debug('Searching for subreddits matching {!r} ...'.format(query))
    headers = {'user-agent': USER_AGENT.format(version=wf.version,
                                               url=wf.help_url)}

    params = {'limit': limit, 'q': query}

    r = web.get(SEARCH_URL, params, headers=headers)
    log.debug('[{}] {}'.format(r.status_code, r.url))

    r.raise_for_status()

    subreddits = r.json()['data']['children']

    subreddits = [handle_subreddit(d) for d in subreddits]
    # Only show public subreddits
    subreddits = [d for d in subreddits if d['type'] == 'public']

    for sr in subreddits:
        log.debug(sr)

    return subreddits

def subreddit(name, limit=POST_COUNT):
    """Return list of hot posts on specified subreddit"""
    log.debug('Fetching hot posts in r/{} ...'.format(name))
    url = POST_URL.format(name=name)
    headers = {'user-agent': USER_AGENT.format(version=wf.version,
                                               url=wf.help_url)}
    params = {'limit': limit}

    log.debug('url : {}'.format(url))
    r = web.get(url, params, headers=headers)
    log.debug('[{}] {}'.format(r.status_code, r.url))

    # API redirects to subreddit search instead of returning a 404 :(
    if r.status_code == 404 or r.url.startswith(SEARCH_URL):
        msg = 'Not a subreddit : `{}`'.format(name)
        log.error(msg)
        return None

    r.raise_for_status()

    data = r.json()
    posts = [handle_post(d) for d in data['data']['children']]

    return posts

def post_search_key(post):
    return '{} {}'.format(post['title'], post['author'])

def subreddit_search_key(sr):
    return '{} {}'.format(sr['name'], sr['title'])

def main(wf):

    from docopt import docopt
    args = docopt(__doc__, wf.args)

    log.debug('args : {!r}'.format(args))

    # Run Script actions
    # ------------------------------------------------------------------

    if args.get('<postdata>'):

        d = json.loads(args.get('<postdata>'))

        if args.get('--openurl'):
            url = d['url']
        elif args.get('--opencomments'):
            url = d['permalink']

        log.debug('Opening : {}'.format(url))
        subprocess.call(['open', url])

        return 0

    ####################################################################
    # Script Filter
    ####################################################################

    # Updates
    # ------------------------------------------------------------------

    if wf.update_available:
        wf.add_item('A newer version is available',
                    '↩ to install update',
                    autocomplete='workflow:update',
                    icon=ICON_UPDATE)

    query = args.get('<query>')

    log.debug('query : {!r}'.format(query))

    # Show popular subreddits
    # ------------------------------------------------------------------

    if query == '':

        subreddits = wf.cached_data('--popular', popular_subreddits,
                                    max_age=CACHE_MAX_AGE)

        for sr in subreddits:

            wf.add_item(sr['name'],
                        sr['title'],
                        autocomplete='{}/'.format(sr['name']),
                        icon=ICON_REDDIT)

        wf.send_feedback()
        return 0

    # Parse query
    # ------------------------------------------------------------------

    # r/blah -> Search for subreddit matching `blah`
    # r/blah/ -> List hot posts in r/blah
    # r/blah/wut -> Filter hot posts in r/blah by `wut`
    m = re.match('([^/]+)(/)?(.+)?', query)

    if not m:
        wf.add_item('Invalid query',
                    'Try a different query',
                    icon=ICON_WARNING)
        wf.send_feedback()
        return 0

    name, slash, query = m.groups()

    log.debug('name : {!r} slash : {!r}  query : {!r}'.format(
              name, slash, query))

    # Search for matching subreddit
    # ------------------------------------------------------------------

    if not slash:

        key = '--search-{}'.format(cache_key(name))

        subreddits = wf.cached_data(key, lambda: search_subreddits(name),
                                    max_age=CACHE_MAX_AGE)

        # Filter results because Reddit's search is super-crappy
        subreddits = wf.filter(name, subreddits,
                               key=subreddit_search_key,
                               min_score=30)

        if not subreddits:

            wf.add_item('No matching subreddits found',
                        'Try a different query',
                        icon=ICON_WARNING)

        else:  # List all matching subreddits

            for sr in subreddits:

                wf.add_item(sr['name'],
                            sr['title'],
                            autocomplete='{}/'.format(sr['name']),
                            icon=ICON_REDDIT)

        wf.send_feedback()
        return 0

    # Browse/search within subreddit
    # ------------------------------------------------------------------

    # Filesystem-friendly key
    key = cache_key(name)

    log.debug('Viewing r/{} ...'.format(name))

    posts = wf.cached_data(key, lambda: subreddit(name), max_age=CACHE_MAX_AGE)

    if posts is None:  # Non-existent subreddit
        wf.add_item('r/{} does not exist'.format(name),
                    'Try a different name',
                    icon=ICON_WARNING)

        wf.send_feedback()
        return 0

    if query:
        posts = wf.filter(query, posts, key=post_search_key, min_score=30)

    if not posts:
        wf.add_item('No matching results found',
                    'Try a different subreddit and/or query',
                    icon=ICON_WARNING)

    for post in posts:
        subtitle = 'Posted {} by {} // {}'.format(post['reltime'],
                                                  post['author'],
                                                  post['url'])

        # Encode arg to send to Run Script
        arg = json.dumps({'url': post['url'], 'permalink': post['permalink']})

        modifiers = {}

        if post['url'] != post['permalink']:  # Not a Self post
            modifiers['cmd'] = 'View Comments on Reddit'
        else:
            modifiers['cmd'] = '[Self post] View on Reddit'

        wf.add_item(post['title'],
                    subtitle,
                    modifier_subtitles=modifiers,
                    arg=arg,
                    largetext=post['title'],
                    valid=True,
                    icon=ICON_REDDIT)

    wf.send_feedback()

    log.debug('{} hot posts in subreddit `{}`'.format(len(posts), name))

if __name__ == '__main__':
    wf = Workflow(help_url=HELP_URL,
                  update_settings=UPDATE_SETTINGS)
    log = wf.logger
    sys.exit(wf.run(main))


""" Graph is a data structure can be traversed by
 two methods breadth first traversal
 and depth first traversal"""
# Depth First traversal requires stack to implement

from collections import defaultdict
class Graph:
    # constructor
    def __init__(self):
        self.graph = defaultdict(list)

    def addEdge(self, u, v):
        self.graph[u].append(v)

    def DFSUtil(self, v, visited):
        visited.add(v)
        print(v, end=' ')

        for neighbour in self.graph[v]:
            if neighbour in self.graph[v]:
                if neighbour not in visited:
                    self.DFSUtil(neighbour, visited)

    def  DFS(self, v):
        visited = set()
        self.DFSUtil(v, visited)

# Driver Code:
g = Graph()
g.addEdge(0, 1)
g.addEdge(0, 2)
g.addEdge(1, 2)
g.addEdge(2, 0)
g.addEdge(2, 3)
g.addEdge(3, 3)
print("Following is dfs from starting from vertex 2")
g.DFS(2)

Graph algorithms/Depth first traversal.py


num = int(input("enter any number : "))
temp = num
num1=0
while(num>0):
    rem  = num%10
    num1 = num1*10+rem
    num = num // 10
if num1 == temp:
    print("yes, given number is palindrom ")

else:
    print("No, given number is not palindrom")


from django.urls import path
from .views import *
from django.contrib.auth import views as auth_views

urlpatterns = [
    path('', home, name = 'home'),
    path('new/', CreateNew.as_view(), name = 'new'),
    path('<int:pk>/', PostDetailView.as_view(), name='detail'),

    path('register/', SingUpView.as_view(), name='register'),
    path('login/', auth_views.LoginView.as_view(
        template_name = 'login1.html'
    ), name='login'),

    path('logout/', auth_views.LogoutView.as_view(
        next_page='/'
    ), name='logout'),

    path('contact/',contact, name='contact')
]


import easyocr
import cv2
import sys

def main():
    reader = easyocr.Reader(['id'],False)
    path_img = sys.argv[1]
    image = cv2.imread(path_img)
    image_gray = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
    cv2.imwrite('./hasil.jpg',image_gray)
    hasil = reader.readtext('./hasil.jpg')
    hasil = ' '.join([row[1] for row in hasil])
    with open(r'C:\Users\alrav\Desktop\OCR_hasil.txt', 'w') as f:
        f.writelines(hasil)

if '__main__' == __name__:
    main()


# 2338 긴자리 계산
"""
두 수 a,b를 입력받아, a+b,a-b,a*b를 구하는 프로그램을 작성하시오.

첫째 줄에 a, 둘째 줄에 b가 주어진다.
첫째 줄에 a+b, 둘째 줄에 a-b, 셋째 줄에 a*b를 출력한다.
"""
a = int(input())
b = int(input())
print(a+b, a-b, a*b, sep='\n')


import re
import sys

brasil = [[], []]
chile = [[], []]
mexico = [[], []]

#arquivo recebe o primeiro argumento da linha do script
arquivo = str(sys.argv[1])

#cria o arquivo e grava nele a saida do programa
sys.stdout = open("resultado.txt", "a")

#verifica se caso o arquivo passado, nao tiver o .txt, terá acesso do mesmo jeito
if not '.' in arquivo:
    arquivo += '.txt'

#abre o arquivo para leitura e atraves do open foi iterado linha por linha
with open(arquivo, 'r') as f:
    for line in f:

        if re.search('^55', line): #verifica se no comeco da frase tem 55
           brasil[0].append(line) #adiciona a linha na lista brasil[0]
           if(re.findall('assinado', line)): #verifica se na linha tem assinado
               brasil[1].append(line) #adiciona a linha na lista brasil[1]

        if re.search('^56', line):
           chile[0].append(line)
           if(re.findall('assinado', line)):
               chile[1].append(line)

        if re.search('^52', line):
           mexico[0].append(line)
           if(re.findall('assinado', line)):
               mexico[1].append(line)

print("Brasil, " + str(len(brasil[0])) + ", " + str(len(brasil[1])))
print("Chile, " + str(len(chile[0])) + ", " + str(len(chile[1])))
print("Mexico, " + str(len(mexico[0])) + ", " + str(len(mexico[1])))


__title__ = "Solver z88 FEM unit tests"
__author__ = "Bernd Hahnebach"
__url__ = "https://www.freecad.org"

import unittest
from os import listdir
from os.path import join
from os.path import isfile

import FreeCAD

import femsolver.run
from . import support_utils as testtools
from .support_utils import fcc_print
from .support_utils import get_namefromdef

class TestSolverZ88(unittest.TestCase):
    fcc_print("import TestSolverZ88")

    # ********************************************************************************************
    def setUp(
        self
    ):
        # setUp is executed before every test

        # new document
        self.document = FreeCAD.newDocument(self.__class__.__name__)

        # more inits
        self.mesh_name = "Mesh"

    # ********************************************************************************************
    def tearDown(
        self
    ):
        # tearDown is executed after every test
        FreeCAD.closeDocument(self.document.Name)

    # ********************************************************************************************
    def test_00print(
        self
    ):
        # since method name starts with 00 this will be run first
        # this test just prints a line with stars

        fcc_print("\n{0}\n{1} run FEM TestSolverFrameWork tests {2}\n{0}".format(
            100 * "*",
            10 * "*",
            55 * "*"
        ))

    # ********************************************************************************************
    def test_ccx_cantilever_ele_hexa20(
        self
    ):
        from femexamples.ccx_cantilever_ele_hexa20 import setup
        setup(self.document, "z88")
        self.inputfile_writing_test(get_namefromdef("test_"))

    # ********************************************************************************************
    def test_ccx_cantilever_ele_tria6(
        self
    ):
        # TODO does pass on my local machine, but not on ci
        return

        from femexamples.ccx_cantilever_ele_tria6 import setup
        setup(self.document, "z88")
        self.inputfile_writing_test(get_namefromdef("test_"))

    # ********************************************************************************************
    def test_ccx_cantilever_faceload(
        self
    ):
        from femexamples.ccx_cantilever_faceload import setup
        setup(self.document, "z88")
        self.inputfile_writing_test(get_namefromdef("test_"))

    # ********************************************************************************************
    def test_ccx_cantilever_nodeload(
        self
    ):
        from femexamples.ccx_cantilever_nodeload import setup
        setup(self.document, "z88")
        self.inputfile_writing_test(get_namefromdef("test_"))

    # ********************************************************************************************
    def inputfile_writing_test(
        self,
        base_name
    ):
        self.document.recompute()

        # start
        fcc_print(
            "\n------------- Start of FEM Z88 tests for {} -------"
            .format(base_name)
        )

        # get analysis working directory and save FreeCAD file
        working_dir = testtools.get_fem_test_tmp_dir("solver_z88_" + base_name)
        save_fc_file = join(working_dir, base_name + ".FCStd")
        fcc_print("Save FreeCAD file to {} ...".format(save_fc_file))
        self.document.saveAs(save_fc_file)

        # write input file
        machine = self.document.SolverZ88.Proxy.createMachine(
            self.document.SolverZ88,
            working_dir,
            True  # set testmode to True
        )
        machine.target = femsolver.run.PREPARE
        machine.start()
        machine.join()  # wait for the machine to finish

        # compare created input files with the given input files
        test_path = join(testtools.get_fem_test_home_dir(), "z88", base_name)
        test_files = [f for f in listdir(test_path) if isfile(join(test_path, f))]
        # fcc_print(sorted(test_files))
        not_files = ["__init__.py"]
        test_files = [f for f in test_files if f not in not_files]
        fcc_print((test_files))
        for test_file in test_files:
            inpfile_given = join(
                test_path,
                test_file
            )
            inpfile_totest = join(
                working_dir,
                test_file
            )
            fcc_print(
                "Comparing {}  to  {}"
                .format(inpfile_given, inpfile_totest)
            )
            ret = testtools.compare_inp_files(
                inpfile_given,
                inpfile_totest
            )
            self.assertFalse(
                ret,
                "Z88 write_inp_file for {0} test failed.\n{1}".format(base_name, ret)
            )

        # end
        fcc_print(
            "--------------- End of FEM Z88 tests for {} ---------"
            .format(base_name)
        )


def numeric_attribute_histogram(plt, frame, title, num=10, interquantile=False, mmm=True, binary=False):
    """Plot a histogram of an attribute in a DataFrame

    :param plt: plot
    :param frame: pandas DataFrame
    :param title: title on plot and file name
    :param num: num for linspace, will be number of bins in histogram
    :param interquantile: plot interquantile
    :param mmm: plot text with min, max, mean
    :param binary: mmm cannot be True, and num is 2, histogram bars are a legend is added
    """

    plt.close()
              # Not sure if this is clever, but easier unless we plot more
              # things on top of eachother
    fig = plt.figure(1)

    if interquantile:
        title += "\ninterquantile"
    plt.suptitle(title, fontsize=14, fontweight='bold')
    plt.ylabel('sites')
    plt.xlabel('links')

    # Remove missing values
    clean_frame = frame[frame.notnull()]
    clean_frame = np.sort(clean_frame)
    missing_values = len(frame) - len(clean_frame)

    # If any data was removed, we report this on the plot
    if missing_values != 0:
        title += "\n" + str(missing_values) + " missing values"
    vals = clean_frame.values

    # Interquantile values
    if interquantile:
        vals = vals[len(vals) / 4: len(vals) - len(vals) / 4]
    min_ = vals[0]
    max_ = vals[-1]

    # There can be no other way..
    if binary:
        num = 2

    bins = np.linspace(min_, max_, num=num)
    plt.xticks(bins)

    if not binary:
        # Build tables displaying top and bottom values
        bottom_ten_table = build_column_table(clean_frame[:10], "Bottom 10")
        # Top 10 is the end of the list - revesed to have the higheset at the
        # top
        top_ten_table = build_column_table(clean_frame[-10:][::-1], "Top 10")

        # Plot tables
        table_plot = fig.add_subplot(211)
        table_plot.text(0, 0, bottom_ten_table, size=12)
        table_plot.text(0.5, 0, top_ten_table, size=12)

        # General statistical figures
        mean = np.mean(vals)
        mode = scipy.stats.mode(vals)  # mode, and occurances
        median = vals[len(vals) / 2]

        # Build string
        text = "Stat: \n$min=" + str(
            min_) + "$\n" + "$max=" + str(max_) + "$\n"
        text += "$\mu=%.2f$\n" % mean
        text += "$mode=%d (%d)$\n" % (mode[0], mode[1])  # mode, and occurances
        text += "$median=%d$" % median

        # Plot
        fig.text(0.75, 0.75, text, bbox={
                 'facecolor': 'white', 'alpha': 1., 'pad': 8})
        plt.hist(vals, bins=bins)

    else:  # Binary plot
        plt.hist(vals, bins=bins, histtype='bar', color=[
                 'red', 'blue'], label=['No', 'Yes'])

    title = title.lower().replace(' ', '_').replace('\n', '_')
    savefig("./figures/" + title.lower().replace(' ', '_') + ".png")


from model import utils

# sets bot command prefix
if __name__ == "__main__":

    while True:
        prefix = input("Input one letter prefix to invoke bot command:")
        if len(prefix) == 1:
            break
        print("Inputted prefix was not one letter long, try again")

    with open(utils.PREFIX_DIR, 'w') as file:
        file.write(str(prefix))
        print("Prefix " + prefix + " successfully set")


from matplotlib.pyplot import title
import kivy
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.gridlayout import GridLayout
from kivy_garden.xcamera import XCamera
from kivy.uix.camera import Camera
from kivy.uix.popup import Popup
from kivy.uix.image import Image
from kivy.uix.textinput import TextInput
from kivy.properties import ObjectProperty
from kivy.properties import BooleanProperty, StringProperty
from kivy.uix.filechooser import FileChooser
import sample
import connection
import question_answer
import pandas as pd

conn = None
cur = None
menu = None
img_file_name = ""

file_path = 'img1.jpg'
file = file_path[:-4]
file_folder_path = '/Users/anupamadeo/project/mtechproject/backend/images/'
json_folder_path = '/Users/anupamadeo/project/mtechproject/backend/json/'
menu_folder_path = '/Users/anupamadeo/project/mtechproject/backend/dataframe/'
file_path = file_folder_path + file + '.jpg'
json_path = json_folder_path + file + '.json'
menu_path = menu_folder_path + file + '_menu' + '.csv'
menu_dish = menu_folder_path + file + '_menu_dish' + '.csv'
menu_price = menu_folder_path + file + '_menu_price' + '.csv'
menu_dish_type = menu_folder_path + file + '_menu_dish_type' + '.csv'
ner_model_path = '/Users/anupamadeo/project/mtechproject/backend/ner_model'

class MainWindow(Screen):
    pass

class SecondWindow(Screen):
    pass

class MiddleWindow(Screen):

    def image_process(self):
        print('image processing started.........')
        global conn, menu, cur

        print('file_path : ', file_path)
        sample.get_ocr(file_path)
        file = file_path[-8:-4]
        print('file : ', file)

        sample.get_ocr(img_file_name)
        file = file_path[-8:-4]
        print('the name of the file is : ', file)
        sample.get_dataframe(ner_model_path, json_path,
                             menu_dish, menu_price, menu_dish_type)
        sample.make_dataframe(menu_dish, menu_price, menu_dish_type)
        sample.get_dish_types(menu_path, menu_dish_type)
        menu = pd.read_csv(menu_path)
        conn, cur = connection.create_connection()
        print('connection : ', conn, ' cursor  =  ', cur)
        connection.create_database(cursor=cur)
        connection.use_db(cursor=cur)
        connection.create_table(cursor=cur)
        connection.insert_values(connection=conn, cursor=cur, menu=menu)
        print('_______')

    def selected(self, filename):
        global img_file_name
        try:
            self.ids.image2.source = filename[0]
            img_file_name = filename[0]

        except:
            pass

    def next(self):
        show_popup().open()

class ThirdWindow(Screen):
    data_items = StringProperty()

    query = ObjectProperty(None)
    answer = ObjectProperty(None)

    def btn(self):
        self.data_items = ""
        question = self.query.text
        sql_query = question_answer.find_query(menu, cur, question)
        result = connection.execute_query(cur, sql_query)
        print('required answer is : ')
        data = ' '
        for row in result:
            for col in row:
                self.data_items = self.data_items + "   " + str(col)

    def btn1(self):
        self.query.text = " "
        self.answer.text = " "

    def remove_connection(self):
        connection.delete_table(cur)
        connection.delete_database(cur)
        print('done')

    def next1(self):
        show_popup1().open()

class WindowManager(ScreenManager):
    pass

class P(FloatLayout):
    pass

class P1(FloatLayout):
    def remove_connection(self):
        connection.delete_table(cur)
        connection.delete_database(cur)
        print('done')

kv = Builder.load_file('my.kv')

class MyMainApp(App):

    def build(self):
        global menu_path, conn, cur, menu
        return kv

def show_popup():
    show = P()
    popupWindow = Popup(title="Message", content=show,
                        size_hint=(None, None), size=(420, 400))
    return popupWindow

def show_popup1():
    show = P1()
    popupWindow = Popup(title="Message", content=show,
                        size_hint=(None, None), size=(420, 400))
    return popupWindow

if __name__ == '__main__':
    MyMainApp().run()


import sys

import click

from gitflow_toolbox.common.gitlab import CurrentGitlab, RemoteGitlab
from gitflow_toolbox.common.is_main_call import is_main_call

@click.command()
@click.argument("source_branch", type=str)
@click.argument("target_branch", type=str)
@click.option(
    "--remote/--current",
    "-r/-c",
    default=False,
    help="Flag which allow you to chose between configured target or remote to run this tool."
    " By default, the script use --current flag.",
)
@click.option(
    "--state",
    "-s",
    type=click.Choice(["opened", "closed", "locked", "merged"], case_sensitive=False),
    default="opened",
    help="Flag which allow you to filter merge request during search if it exists by the given state."
    " By default to 'opened'.",
)
@click.pass_context
def check_mr_exists(
    ctx: click.Context, source_branch: str, target_branch: str, remote: bool = False, state: str = "opened"
):
    """Check if the merge request between SOURCE_BRANCH and TARGET_BRANCH exists

    Return True if a MR exists (exit code 0), False otherwise (exit code 1)
    \f

    Args:
        source_branch (str): branch to create
        remote_target_branch (str): branch reference to create branch from
        remote (bool, optional):
            whether to check on the current gitlab or remote gitlab (True=remote). Defaults to False.
        state (str, optional): The state of the merge request expected. Defaults to "opened".

    Returns:
        bool: True if a MR exists (exit code 0), False otherwise (exit code 1)
    """
    click.echo(f"Checking if an opened merge request from {source_branch} to {target_branch} exists...")
    project = RemoteGitlab().project if remote else CurrentGitlab().project
    mrs = project.mergerequests.list(state=state.lower(), source_branch=source_branch, target_branch=target_branch)
    mr_exists = len(mrs) != 0
    if is_main_call(ctx):
        click.echo(mr_exists)
        sys.exit(0 if mr_exists else 1)
    return mr_exists


r"""
The source file of the quleaf backend.
"""

import copy
import math
import numpy as np
import paddle
import QCompute
import re
import paddle_quantum as pq
from QCompute import MeasureZ, RX, RY
from typing import Optional, List

BACKEND = 'local_baidu_sim2'
TOKEN = None
QCompute.Define.Settings.outputInfo = False
QCompute.Define.Settings.drawCircuitControl = []

def set_quleaf_backend(backend: str) -> None:
    r"""Set the backend of the QuLeaf.

    Args:
        backend: The backend you want to set.
    """
    global BACKEND
    BACKEND = backend

def get_quleaf_backend() -> str:
    r"""Get the current backend of the QuLeaf.

    Returns:
        Current backend of the QuLeaf.
    """
    if isinstance(BACKEND, str):
        return QCompute.BackendName(BACKEND)
    return BACKEND

def set_quleaf_token(token: str) -> None:
    r"""Set the token of the QuLeaf.

    You need to input your token if you want tu use the cloud server.

    Args:
        token: Your token.
    """
    global TOKEN
    TOKEN = token
    QCompute.Define.hubToken = token

def get_quleaf_token() -> str:
    r"""Get the token you set.

    Returns:
        The token you set.
    """
    return TOKEN

single_qubit_gates = {
    'S': QCompute.S, 'T': QCompute.T, 'Sdg': QCompute.SDG, 'Tdg': QCompute.TDG,
    'H': QCompute.H, 'X': QCompute.X, 'Y': QCompute.Y, 'Z': QCompute.Z,
    'U3': QCompute.U, 'RX': QCompute.RX, 'RY': QCompute.RY, 'RZ': QCompute.RZ
}
multi_qubits_gates = {
    'CNOT': QCompute.CX, 'CX': QCompute.CX, 'CY': QCompute.CY, 'CZ': QCompute.CZ, 'SWAP': QCompute.SWAP,
    'CU': QCompute.CU, 'CRX': QCompute.CRX, 'CRY': QCompute.CRY, 'CRZ': QCompute.CRZ,
    'CSWAP': QCompute.CSWAP, 'CCX': QCompute.CCX
}
fixed_gates = {'S', 'T', 'Sdg', 'Tdg', 'H', 'X', 'Y', 'Z', 'CNOT', 'CX', 'CY', 'CZ', 'SWAP', 'CSWAP', 'CCX'}
parameterized_gates = {'U3', 'RX', 'RY', 'RZ', 'CU', 'CRX', 'CRY', 'CRZ'}

# TODO: need to check whether the logic is right when the param_sharing is True.
def _act_gates_to_state(
        oper_history: List[dict], quleaf_state: QCompute.QEnv,
        _gate_idx: Optional[int] = None, _depth_idx: Optional[int] = None, _qubits_idx: Optional[int] = None,
        _param_idx: Optional[int] = None, _param_shift_val: Optional[int] = None
) -> QCompute.QEnv:
    r"""The function to act the quantum gate to the quantum state in the QuLeaf backend.

    Args:
        oper_history: The history of quantum gate, which records the type, parameters and qubits index of the gates.
        quleaf_state: The quantum state in QuLeaf.
        param_all: All the parameters in the gates.

    Raises:
        NotImplementedError: Some quantum gate is not supported in QuLeaf yet.

    Returns:
        The acted quantum state.
    """
    for gate_idx, gate_info in enumerate(oper_history):
        gate_name = gate_info['name']
        if gate_name in single_qubit_gates:
            gate_func = single_qubit_gates[gate_name]
            if gate_name in fixed_gates:
                for _ in range(gate_info['depth']):
                    for qubit_idx in gate_info['qubits_idx']:
                        gate_func(quleaf_state.Q[qubit_idx])
            elif gate_name == 'U3':
                for depth_idx in range(gate_info['depth']):
                    for idx, qubit_idx in enumerate(gate_info['qubits_idx']):
                        if gate_info['param_sharing']:
                            param = gate_info['param'][depth_idx].tolist()
                        else:
                            param = gate_info['param'][depth_idx][idx].tolist()
                        if gate_idx == _gate_idx and depth_idx == _depth_idx and idx == _qubits_idx:
                            param[_param_idx] += _param_shift_val
                        gate_func(*param)(quleaf_state.Q[qubit_idx])
            else:
                # Single qubit parameterized quantum gate
                for depth_idx in range(gate_info['depth']):
                    for idx, qubit_idx in enumerate(gate_info['qubits_idx']):
                        if gate_info['param_sharing']:
                            param = gate_info['param'][depth_idx][0].item()
                        else:
                            param = gate_info['param'][depth_idx][idx][0].item()
                        if gate_idx == _gate_idx and depth_idx == _depth_idx and idx == _qubits_idx:
                            param += _param_shift_val
                        gate_func(param)(quleaf_state.Q[qubit_idx])
        elif gate_name in multi_qubits_gates:
            gate_func = multi_qubits_gates[gate_name]
            if gate_name in fixed_gates:
                for _ in range(gate_info['depth']):
                    for qubits_idx in gate_info['qubits_idx']:
                        gate_func(*[quleaf_state.Q[qubit_idx] for qubit_idx in qubits_idx])
            elif gate_name == 'CU':
                for depth_idx in range(gate_info['depth']):
                    for idx, qubits_idx in enumerate(gate_info['qubits_idx']):
                        if gate_info['param_sharing']:
                            param = gate_info['param'][depth_idx].tolist()
                        else:
                            param = gate_info['param'][depth_idx][idx].tolist()
                        if gate_idx == _gate_idx and depth_idx == _depth_idx and idx == _qubits_idx:
                            param[_param_idx] += _param_shift_val
                        gate_func(*param)(*[quleaf_state.Q[qubit_idx] for qubit_idx in qubits_idx])
            else:
                # CRx, CRy, CRz
                for depth_idx in range(gate_info['depth']):
                    for idx, qubits_idx in enumerate(gate_info['qubits_idx']):
                        if gate_info['param_sharing']:
                            param = gate_info['param'][depth_idx][0].item()
                        else:
                            param = gate_info['param'][depth_idx][idx][0].item()
                        if gate_idx == _gate_idx and depth_idx == _depth_idx and idx == _qubits_idx:
                            param += _param_shift_val
                        gate_func(param)(*[quleaf_state.Q[qubit_idx] for qubit_idx in qubits_idx])
        else:
            raise NotImplementedError
    return quleaf_state

def _expec_val_on_quleaf(state: 'QCompute.QEnv', coeff: 'float', pauli_str: 'str', shots: 'int') -> float:
    r"""Compute the expectation value of the observable with respect to the input state in the QuLeaf backend.

    Args:
        state: The quantum state in the QuLeaf backend.
        coeff: The coefficient value of the pauli string.
        pauli_str: The pauli string, which is a term in hamiltonian.
        shots: The number of measurement shots.

    Raises:
        ValueError: The pauli string should be legal.

    Returns:
        The expectation value of the observable with respect to the input quantum state.
    """
    if pauli_str.lower() == 'i':
        return coeff
    pauli_terms = re.split(r',\s*', pauli_str.lower())
    observed_qubits = []
    # _state = state
    _state = copy.deepcopy(state)
    for pauli_term in pauli_terms:
        pauli_matrix = pauli_term[0]
        qubit_idx = int(pauli_term[1:])
        observed_qubits.append(qubit_idx)
        if pauli_matrix == 'x':
            RY(-math.pi / 2)(_state.Q[qubit_idx])
        elif pauli_matrix == 'y':
            RX(math.pi / 2)(_state.Q[qubit_idx])
        elif pauli_matrix != 'z':
            raise ValueError("Cannot recognize the pauli words of the hamiltonian.")
    MeasureZ(*_state.Q.toListPair())
    counts = _state.commit(shots, fetchMeasure=True)['counts']
    filtered_counts = [(counts[key], [key[-idx - 1] for idx in observed_qubits]) for key in counts]
    return coeff * sum(
        (
            ((-1) ** key.count('1')) * val / shots
            for val, key in filtered_counts
        )
    )

class ExpecValOp(paddle.autograd.PyLayer):
    @staticmethod
    def forward(
            ctx: paddle.autograd.PyLayerContext,
            state: 'pq.State',
            hamiltonian: 'pq.Hamiltonian',
            shots: paddle.Tensor,
            *parameters
    ) -> paddle.Tensor:
        r"""The forward function to compute the expectation value of the observable in the QuLeaf Backend.

        Args:
            ctx: To save some variables so that they can be used in the backward function.
            param: The parameters in the previous quantum gates.
            state: The quantum state to be measured.
            hamiltonian: The observable.
            shots: The number of measurement shots.
            *parameters: The parameters in the parameterized quantum circuit.

        Returns:
            The expectation value of the observable for the input state.
        """
        dtype = shots.dtype
        shots = int(shots.item())
        ctx.save_for_backward(*parameters)
        quleaf_state = copy.deepcopy(state.data)
        oper_history = state.oper_history
        ctx.quleaf_state = quleaf_state
        ctx.oper_history = oper_history
        ctx.hamiltonian = hamiltonian
        ctx.shots = shots
        ctx.dtype = dtype
        _state = copy.deepcopy(quleaf_state)
        acted_state = _act_gates_to_state(oper_history, _state, parameters)
        expec_val = 0
        for coeff, pauli_str in hamiltonian.pauli_str:
            _state = copy.deepcopy(acted_state)
            expec_val += _expec_val_on_quleaf(_state, coeff, pauli_str, shots)
        expec_val = paddle.to_tensor([expec_val], dtype=dtype)
        return expec_val

    @staticmethod
    def backward(ctx: paddle.autograd.PyLayerContext, expec_val_grad: paddle.Tensor) -> paddle.Tensor:
        r"""The backward function which is to compute the gradient of the input parameters.

        Args:
            ctx: To get the variables saved in the forward function.
            expec_val_grad: The gradient of the expectation value.

        Returns:
            The gradient of the parameters for the quantum gates.
        """
        parameters = ctx.saved_tensor()
        dtype = ctx.dtype
        quleaf_state = ctx.quleaf_state
        oper_history = ctx.oper_history
        hamiltonian = ctx.hamiltonian
        shots = ctx.shots

        def expec_val_shift(
                _gate_idx: int, _depth_idx: int, _qubits_idx: int, _param_idx: int, param_shift_val: float
        ) -> float:
            _state = copy.deepcopy(quleaf_state)
            acted_state = _act_gates_to_state(
                oper_history, _state, _gate_idx, _depth_idx, _qubits_idx, _param_idx, param_shift_val)
            expec_val = 0
            for coeff, pauli_str in hamiltonian.pauli_str:
                _state = copy.deepcopy(acted_state)
                expec_val += _expec_val_on_quleaf(_state, coeff, pauli_str, shots)
            # expec_val = paddle.to_tensor([expec_val], dtype=expec_val_grad.dtype)
            return expec_val

        def general_param_shift(
                gate_name: str, _gate_idx: int, _depth_idx: int, _qubits_idx: int, _param_idx: int
        ) -> float:
            if gate_name in {'CRX', 'CRY', 'CRZ', 'CU'}:
                coeff_list = [
                    1 / (16 * math.pow(math.sin(math.pi / 8), 2)),
                    -1 / (16 * math.pow(math.sin(3 * math.pi / 8), 2)),
                    1 / (16 * math.pow(math.sin(5 * math.pi / 8), 2)),
                    - 1 / (16 * math.pow(math.sin(7 * math.pi / 8), 2)),
                ]
                shift_list = [math.pi / 2, 3 * math.pi / 2, 5 * math.pi / 2, 7 * math.pi / 2]
                grad_terms = map(
                    lambda coeff, shift_val: coeff * expec_val_shift(
                        _gate_idx, _depth_idx, _qubits_idx, _param_idx, shift_val
                    ),
                    coeff_list, shift_list
                )
                grad = sum(grad_terms)
            else:
                coeff1 = 0.5
                coeff2 = -0.5
                grad = (
                    coeff1 * expec_val_shift(_gate_idx, _depth_idx, _qubits_idx, _param_idx, math.pi / 2) +
                    coeff2 * expec_val_shift(_gate_idx, _depth_idx, _qubits_idx, _param_idx, 3 * math.pi / 2)
                )
            return grad

        params_idx = 0
        params_grad = [None]
        for gate_idx, gate_info in enumerate(oper_history):
            gate_name = gate_info['name']
            if gate_name not in parameterized_gates:
                continue
            if parameters[params_idx].stop_gradient is True:
                params_grad.append(None)
                params_idx += 1
                continue
            shape = parameters[params_idx].shape
            param_grad = np.zeros(shape=shape)
            for _depth_idx in range(shape[0]):
                for _qubits_idx in range(shape[1]):
                    for _param_idx in range(shape[2]):
                        grad = general_param_shift(gate_name, gate_idx, _depth_idx, _qubits_idx, _param_idx)
                        param_grad[_depth_idx, _qubits_idx, _param_idx] += grad
            params_grad.append(expec_val_grad * paddle.to_tensor(param_grad, dtype=dtype))
            params_idx += 1
        return params_grad


#!/home/wiseman/anaconda3/bin/python
# -*- coding: utf-8 -*-
'''Script to find the photometry for a certain supernova'''
# exectuable to run through the entire stack process
import numpy as np
import pandas as pd
import logging
import argparse
from time import gmtime, strftime
from astropy.coordinates import SkyCoord
from astropy import units as u
from astropy.table import Table
import astropy.io.fits as fits
import os
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
import _pickle as cpickle
import math
import glob
import seaborn as sns
import itertools

sns.set_color_codes(palette='colorblind')

plot_locs_labeled={
'g':[0.18,0.53,0.40,0.43],
'r':[0.59,0.53,0.40,0.43],
'i':[0.18,0.09,0.40,0.43],
'z':[0.59,0.09,0.40,0.43]
}

plot_locs_paper = {
'g':[0.02,0.51,0.47,0.47],
'r':[0.51,0.51,0.47,0.47],
'i':[0.02,0.02,0.47,0.47],
'z':[0.51,0.02,0.47,0.47]
}

bands = ['g','r','i','z']
pix_arcsec = 0.264
def parser():
    parser = argparse.ArgumentParser()
    parser.add_argument('-n','--sn_name',help='Full DES supernova ID, e.g. DES16C2nm (case sensitive)',default=None)
    parser.add_argument('-ra','--ra',help='RA of target',default=None)
    parser.add_argument('-de','--dec',help='DEC of target',default=None)
    parser.add_argument('-l','--namelist',help='List of SN names as a .txt or .csv file',default = None)
    parser.add_argument('-wd','--workdir',help='Path to directory to work in',required = False,default = '/media/data3/wiseman/des/coadding')
    parser.add_argument('-d','--distance',help='Maximum allowable match radius (arcsec) for galaxy association',required=False,default=5)
    parser.add_argument('-s','--stamp',help='Produce a stamp',action ='store_true')
    parser.add_argument('-ss','--size',help='Stamp size (arcsec)',default=30,type=float)
    parser.add_argument('-sh','--show',help='Show image now', action ='store_true')
    parser.add_argument('-p','--path',help='Full path to output image if you are making a stamp',default = 'sn_dir')
    parser.add_argument('-vm','--vmin',help='vmin for greyscale',default=-0.8)
    parser.add_argument('-vx','--vmax',help='vmax for greyscale',default=15.0)
    parser.add_argument('-ne','--new',help = 'Use new stacks?',action='store_true' )
    parser.add_argument('-fc','--finder',help = 'Is this a finder chart?',action='store_true')
    parser.add_argument('-pa','--paper',help = 'Is this for a paper?',action='store_true')
    parser.add_argument('-re','--resfile',help = 'File to find host phot results for this SN',default = None)
    parser.add_argument('-b','--band',help='Only use one band? If so, enter here',default='All')
    parser.add_argument('-f','--ftype',help='File type for save. Default = pdf',default='pdf')
    parser.add_argument('-w','--width',help='Size in pixels',default=300)

    return parser.parse_args()

def get_sn_dat(sn):
    f=open('/media/data3/wiseman/des/coadding/config/chiplims.pkl','rb')
    chiplims = cpickle.load(f)
    sncand = pd.read_csv('/media/data3/wiseman/des/coadding/catalogs/sncand_db.csv')

    dat = sncand[sncand['transient_name']==sn]

    ra,dec =dat[['ra','dec']].iloc[0].values
    y = dat['season'].values[0]

    #################
    obj_field = sn[5:7]
    the_field = chiplims[obj_field]
    for ccd in the_field.keys():
        if the_field[ccd][0][0] > ra > the_field[ccd][2][0]:
            if the_field[ccd][0][1] < dec < the_field[ccd][1][1]:
                return (ra,dec,'SN-%s'%obj_field,y,ccd)

def main(args,logger):
    sn_name = args.sn_name
    match_dist = args.distance

    if sn_name:
        l = [sn_name]
    else:
        l = np.genfromtxt(args.namelist,dtype=str,delimiter='\n')
    for sn in l:
        sn_name = sn
        sn_cap_dir = os.path.join('/media/data3/wiseman/des/coadding/5yr_stacks/CAP/',sn_name)
        logger.info("Locating result file for %s ..."%sn)
        logger.info("First querying the DES database to get the RA,Dec of %s."%sn)
        sn_ra,sn_dec,f,y,chip = get_sn_dat(sn)
        logger.info("%s has the following info:"%sn)
        logger.info("RA:     %s"%sn_ra)
        logger.info("Dec:    %s"%sn_dec)
        logger.info("Season: %s"%y)
        logger.info("Field:  %s"%f)
        logger.info("CCD:    %s"%chip)
        sngals_deep = pd.read_csv('/media/data3/wiseman/des/coadding/results/sngals_deep_v6.csv',index_col=0)
        sn_res = sngals_deep[sngals_deep['TRANSIENT_NAME']==sn]
        has_spec = sn_res[sn_res['SPECZ']>0]
        if len(has_spec)>0:
            loc = has_spec.index
        host = sn_res[sn_res['DLR_RANK']==1]
        phost = sn_res[sn_res['DLR_RANK']==-1]
        if len(host)>0:
            logger.info('Found the host! \n %s'%host)

        logger.info("You want a stamp of %s too. So I'm making one."%sn)
        logger.warning("You might need AplPy installed...")
        import aplpy
        fig,ax = plt.subplots() #figsize=(16,9)
        w = args.size/3600
        ax.set_xticks([])
        ax.set_yticks([])
        for loc in ['top','right','left','bottom']:
            ax.spines[loc].set_visible(False)
        #ax.set_ylabel('Declination (J2000)',fontsize=12,labelpad = 30)
        hor_line = np.array([[sn_ra-(w/7),sn_ra+(w/7)],[sn_dec,sn_dec]])
        ver_line = np.array([[sn_ra,sn_ra],[sn_dec-(w/7),sn_dec+(w/7)]])
        if args.band !='All':
            bands = [args.band]
        else:
            bands = ['g','r','i','z']
        palette = itertools.cycle(sns.color_palette(palette='colorblind',n_colors=5))
        for counter,b in enumerate(bands):

            color = next(palette)
            if counter==1:
                color=next(palette)
            try:
                img_fn = glob.glob(os.path.join(sn_cap_dir,'ccd_*%s*_sci.resamp.fits'%b))[0]
            except:
                from des_stacks import des_stack as stack
                from des_stacks.utils.stack_tools import make_cap_stamps,get_cuts
                cuts = [get_cuts(f,b) for b in bands]
                sg,sr,si,sz = [stack.Stack(f, b, y, [str(chip)] ,'coadding',cuts[counter]) for counter,b in enumerate(bands)]
                make_cap_stamps(sg,sr,si,sz,chip,sn_name,sn_ra,sn_dec,args.width,args.width)
                img_fn = glob.glob(os.path.join(sn_cap_dir,'ccd_*%s*_sci.resamp.fits'%b))[0]

            if os.path.isfile(img_fn):

                sn_res = sn_res[sn_res['RA']<sn_ra+(w)]
                sn_res = sn_res[sn_res['RA']>sn_ra-(w)]
                sn_res = sn_res[sn_res['DEC']<sn_dec+(w)]
                sn_res = sn_res[sn_res['DEC']>sn_dec-(w)]

                img = fits.open(img_fn)
                if not args.paper:
                    plot_locs = plot_locs_labeled
                else:
                    plot_locs = plot_locs_paper

                if args.band != 'All':
                    fg = aplpy.FITSFigure(img,figure=fig)
                else:
                    fg = aplpy.FITSFigure(img,figure=fig,subplot=plot_locs[b])
                try:
                    fg.recenter(sn_ra,sn_dec,w)
                except:
                    logger.info('Could not recenter to outside the frame')

                fg.show_lines([ver_line,hor_line],color=color,linewidth=2.5)
                fg.show_grayscale(vmin=float(args.vmin),vmax=float(args.vmax))
                fg.axis_labels.hide()
                fg.tick_labels.hide()
                fg.set_theme('publication')
                fg.ticks.set_length(0)
                if args.paper:
                    fg.ticks.hide()
                fg.add_label(0.9,0.8,b,relative=True,color=color,fontsize=26,weight='bold')
                fg.add_scalebar(5/3600,color=color,linewidth=3,fontsize=20,weight='bold')
                fg.scalebar.set_label('5"')
                # now add some region ellipses and axis_labels
                try:
                    i = sn_res[(sn_res['RA']<host['RA'].values[0]+0.00001)&(sn_res['RA']>host['RA'].values[0]-0.00001)].index
                    sn_res.drop(i,inplace=True)
                except:
                    pass
                try:
                    j = sn_res[(sn_res['RA']<has_spec['RA'].values[0]+0.00001)&(sn_res['RA']>has_spec['RA'].values[0]-0.00001)].index
                    sn_res.drop(j,inplace=True)
                except:
                    pass

                if not args.finder and not args.paper:
                    try:
                        As,Bs,thetas = sn_res.A_IMAGE.values*pix_arcsec*4/3600,sn_res.B_IMAGE.values*pix_arcsec*4/3600,sn_res.THETA_IMAGE.values
                        ras,decs = sn_res.RA.values,sn_res.DEC.values
                        mags,errs = sn_res['MAG_AUTO_%s'%b.capitalize()].values,sn_res['MAGERR_AUTO_%s'%b.capitalize()].values

                        fg.show_ellipses(ras,decs,As,Bs,thetas,edgecolor='g',facecolor='none',linewidth=1,alpha=.8)
                        if len(host)>0:
                            if not math.isnan(host['SPECZ'].values[0]):
                                fg.show_ellipses(host.RA.values,host.DEC.values,4*host.A_IMAGE.values*pix_arcsec/3600,
    4*host.B_IMAGE.values*pix_arcsec/3600,host.THETA_IMAGE.values,edgecolor='r',facecolor='none',linewidth=1)
                            else:
                                fg.show_ellipses(host.RA.values,host.DEC.values,4*host.A_IMAGE.values*pix_arcsec/3600,
    4*host.B_IMAGE.values*pix_arcsec/3600,host.THETA_IMAGE.values,edgecolor='b',facecolor='none',linewidth=1)
                                fg.add_label(host.RA.values[0],host.DEC.values[0]+0.00045,'%.3f +/- %.3f'%(host['MAG_AUTO_%s'%b.capitalize()].values[0],host['MAGERR_AUTO_%s'%b.capitalize()].values[0]),
                                         size=8,color='b',weight='bold')

                        for obj in range(len(ras)):

                            if decs[obj]+0.00045 < sn_dec+w:

                                fg.add_label(ras[obj],decs[obj]+0.00045,'%.3f +/- %.3f'%(mags[obj],errs[obj]),
                                         size=7,color='g',weight='bold')
                            else:
                                fg.add_label(ras[obj],decs[obj]-0.00045,'%.3f +/- %.3f'%(mags[obj],errs[obj]),
                                         size=7,color='g',weight='bold')
                        for spec in range(len(has_spec.RA.values)):
                            row = has_spec.iloc[spec]
                            if len(host)>0:
                                if row['RA']!=host['RA'].values[0]:
                                    fg.add_label(row.RA,row.DEC+0.001,
                                             'z = %.3g'%has_spec.SPECZ.values[spec],size=7,color='b',weight='bold')
                                    fg.add_label(row.RA,row.DEC+0.00065,'%.3f +/- %.3f'%(row['MAG_AUTO_%s'%b.capitalize()],row['MAGERR_AUTO_%s'%b.capitalize()]),
                                         size=8,color='b',weight='bold')
                                    fg.show_ellipses(row.RA,row.DEC,4*row.A_IMAGE*pix_arcsec/3600,
             4*row.B_IMAGE*pix_arcsec/3600,row.THETA_IMAGE,edgecolor='b',facecolor='none',linewidth=1)
                                else:
                                    fg.add_label(row.RA,row.DEC+0.001,
                                             'z = %.3g'%has_spec.SPECZ.values[spec],size=7,color='r',weight='bold')
                                    fg.add_label(row.RA,row.DEC+0.00065,'%.3f +/- %.3f'%(row['MAG_AUTO_%s'%b.capitalize()],row['MAGERR_AUTO_%s'%b.capitalize()]),
                                         size=8,color='r',weight='bold')

                            else:
                                    fg.add_label(row.RA,row.DEC+0.001,
                                             'z = %.3g'%has_spec.SPECZ.values[spec],size=7,color='b',weight='bold')
                                    fg.add_label(row.RA,row.DEC+0.00065,'%.2f +/- %.2f'%(row['MAG_AUTO_%s'%b.capitalize()],row['MAGERR_AUTO_%s'%b.capitalize()]),
                                         size=8,color='b',weight='bold')

                    except:
                        pass
                '''if counter in [0,2]:
                    fg.tick_labels.show_y()
                if counter in [2,3]:
                    fg.tick_labels.show_x()'''
            else:
                fg = aplpy.FITSFigure('/media/data3/wiseman/des/coadding/config/blank2.fits',figure=fig,subplot=plot_locs[b])
                fg.axis_labels.hide()
                fg.tick_labels.hide()
                fg.add_label(0.5,0.5,'[Failed to load %s band image]'%b,relative=True,fontsize=12,color='black')
            if counter ==0 and not args.paper:
                fg.add_label(0.99,1.05,sn,relative=True,fontsize=14,color='black')
        if args.paper:
            plt.subplots_adjust(left=0.02,right=0.98)
        #plt.suptitle('Right Ascension (J2000)',x=0.57,y=0.04)
        if args.path =='sn_dir':
            savepath =os.path.join(sn_cap_dir,'%s_stamp.%s'%(sn,args.ftype))
        else:
            savepath =os.path.join(args.path,'%s_stamp.%s'%(sn,args.ftype))

        plt.savefig(savepath)
        plt.close(fig)
        logger.info("Figure saved to %s"%savepath)
        logger.info("************* Finished looking up %s! *************"%sn)
if __name__ == "__main__":
    logger = logging.getLogger('sn_host_lookup.py')
    logger.setLevel(logging.DEBUG)
    formatter =logging.Formatter('%(name)s - %(asctime)s - %(levelname)s - %(message)s', datefmt='%Y-%m-%d %H:%M:%S')
    ch = logging.StreamHandler()
    ch.setLevel(logging.DEBUG)
    ch.setFormatter(formatter)
    logger.addHandler(ch)
    logger.info("***********************************")
    logger.info("Initialising *** sn_host_stamp.py *** at %s UT" % strftime("%Y-%m-%d %H:%M:%S", gmtime()))
    logger.info("***********************************")
    args = parser()
    main(args,logger)
    logger.info("************* Finished all SN you gave me! *************")


import base64
import io
from odoo import models

class ReportPatientXls(models.AbstractModel):
    _name = 'report.hospital.report_patient_xls'
    _inherit = 'report.report_xlsx.abstract'

    def generate_xlsx_report(self, workbook, data, patients):
        sheet = workbook.add_worksheet('op')
        head = workbook.add_format(
            {'align': 'center', 'bold': True, 'font_size': '14px'})
        col_head = workbook.add_format(
            {'align': 'center', 'bold': True, 'font_size': '12px'})
        bold = workbook.add_format({'bold': True})
        sheet.set_column('A:A', 6)
        sheet.set_column('B:B', 13)
        sheet.set_column('C:C', 26)
        sheet.set_column('D:D', 12)
        sheet.set_column('E:E', 22)
        sheet.set_column('F:F', 12)
        sheet.set_column('G:G', 15)
        sheet.merge_range('D1:E1', 'MEDICAL REPORT', head)
        form_data = data['form_data']
        if form_data['patient_card_id'] and form_data['patient_name_id']:
            patient_id = form_data['patient_card_id'][1]
            patient_name = form_data['patient_name_id'][1]
            patient_reference = patient_id + '  ' + patient_name
        else:
            patient_reference = ' ' + '  ' + ' '
        sheet.merge_range('C3:E3', patient_reference, bold)
        if form_data['doctor_id']:
            doctor = 'Doctor : ' + form_data['doctor_id'][1]
        else:
            doctor = 'Doctor : ' + ' '
        sheet.merge_range('C5:E5', doctor)
        if form_data['date_from']:
            date_from = 'Date From : ' + form_data['date_from']
        else:
            date_from = 'Date From : ' + ' '
        sheet.merge_range('C6:D6', date_from)
        if form_data['date_to']:
            date_to = 'Date To : ' + form_data['date_to']
        else:
            date_to = 'Date To : ' + ' '
        sheet.merge_range('C7:D7', date_to)
        row = 8
        col = 0
        sheet.write(row, col, 'SL.No', col_head)
        sheet.write(row, col + 1, 'OP Reference', col_head)
        sheet.write(row, col + 2, 'Patient name', col_head)
        sheet.write(row, col + 3, 'Date', col_head)
        sheet.write(row, col + 4, 'Doctor', col_head)
        sheet.write(row, col + 5, 'Department', col_head)
        sheet.write(row, col + 6, 'Disease', col_head)
        count = 0
        for op in data['op']:
            row += 1
            count += 1
            sheet.write(row, col, count)
            sheet.write(row, col + 1, op['op'])
            sheet.write(row, col + 2, op['patient_name'])
            sheet.write(row, col + 3, op['date'])
            sheet.write(row, col + 4, op['doctor'])
            sheet.write(row, col + 5, op['department'])
            sheet.write(row, col + 6, op['disease'])


import requests
from bs4 import BeautifulSoup
from datetime import datetime
import json
import os

class NewsCrawler(object):

	def __init__(self):
		super(NewsCrawler, self).__init__()

	def read_previous(this, website):
		filename = os.path.join('data', website + '.json')
		if os.path.isfile(filename):
			with open(filename, 'r', encoding='utf-8') as f:
				titles = json.load(f)
			os.remove(filename)
		else:
			titles = {}
		return titles

	def write_new_title(this, website, titles):
		filename = os.path.join('data', website + '.json')
		with open(filename, 'w', encoding='utf-8') as f:
			json.dump(titles, f, ensure_ascii=False)

	def chinaNews(this):

		root_url = 'https://www.chinatimes.com'
		url = 'https://www.chinatimes.com/realtimenews/?page={page}&chdtv'
		pre_titles = this.read_previous('chinaNews')
		new_titles = {}

		# go through every pages
		for page in range(1,11):

			html = requests.get(url.format(page=str(page))).text
			html = BeautifulSoup(html, "lxml")

			blocks = html.select('.vertical-list.list-style-none')[0]
			items = blocks.select('li')
			for item in items:
				title = item.select('.title')[0].select('a')[0].text
				link = root_url + item.select('.title')[0].select('a')[0]['href']

				# compare with previous data
				if title not in pre_titles:
					new_titles[title] = link
					print(title)
				else:
					this.write_new_title('chinaNews', new_titles)
					return new_titles

		this.write_new_title('chinaNews', new_titles)
		return new_titles

	def formosa(this):

		root_url = 'https://www.ftvnews.com.tw'
		url = 'https://www.ftvnews.com.tw/tag/快新聞/{page}'
		pre_titles = this.read_previous('formosa')
		new_titles = {}

		# go through every pages
		page = 1
		while True:
			if page > 20:
				break

			html = requests.get(url.format(page=str(page))).text
			html = BeautifulSoup(html, 'lxml')

			items = html.select('.col-lg-4.col-sm-6')
			for item in items:
				title = item.select('.title')[0].text.replace('快新聞／', '')
				link = root_url + item.select('a')[0]['href']
				if title not in pre_titles:
					new_titles[title] = link
					print(title)
				else:
					this.write_new_title('formosa', new_titles)
					return new_titles

			pagiNum = html.select('.pagiNum')[0].text
			pagiNum = pagiNum.split('/')[1]
			pagiNum = int(pagiNum)
			if page == pagiNum:
				break
			else:
				page += 1

		this.write_new_title('formosa', new_titles)
		return new_titles

	def threeStand(this):

		root_url = 'https://www.setn.com'
		url = 'https://www.setn.com/ViewAll.aspx?PageGroupID=5&p={page}'
		pre_titles = this.read_previous('threeStand')
		new_titles = {}

		page = 1
		while True:
			if page > 10:
				break

			html = requests.get(url.format(page=str(page))).text
			html = BeautifulSoup(html, 'lxml')

			items = html.select('.col-sm-12.newsItems')
			if len(items) == 0:
				break

			for item in items:
				title = item.select('.gt')[0].text.replace('\u3000', '')
				link = root_url + item.select('a')[1]['href']
				if title not in pre_titles:
					new_titles[title] = link
					print(title)
				else:
					this.write_new_title('threeStand', new_titles)
					return new_titles
			page += 1

		this.write_new_title('threeStand', new_titles)
		return new_titles

	def unionNews(this):

		root_url = 'https://udn.com'
		url_more = 'https://udn.com/api/more?page={page}&id=&channelId=1&cate_id=0&type=breaknews&totalRecNo={rec}'
		url_page = 'https://udn.com/news/breaknews/1'
		pre_titles = this.read_previous('unionNews')
		new_titles = {}

		# get totalRecNo from html
		html = requests.get(url_page).text
		html = BeautifulSoup(html, 'lxml')
		button = html.select('#indicator')[0]
		query_info = button['data-query'].replace('\'', '"')
		query_info = json.loads(query_info)
		rec = query_info['totalRecNo']
		# print(rec)

		page = 1
		while True:
			try:
				req = requests.get(url_more.format(page=str(page),rec=rec)).json()
			except:
				pass
			datas = req['lists']
			for data in datas:
				title = data['title']
				link = root_url + data['titleLink']
				if title not in pre_titles:
					new_titles[title] = link
					print(title)
				else:
					this.write_new_title('unionNews', new_titles)
					return new_titles

			if req['end']:
				break
			page += 1

		this.write_new_title('unionNews', new_titles)
		return new_titles

	def freeNews(this):

		url = 'https://news.ltn.com.tw/ajax/breakingnews/all/{page}'
		pre_titles = this.read_previous('freeNews')
		new_titles = {}

		page = 2
		while True:

			datas = requests.get(url.format(page=str(page))).json()['data']
			if len(datas) == 0:
				break

			for data in datas:
				title = datas[data]['title']
				link = datas[data]['url']

				if title not in pre_titles:
					new_titles[title] = link
					print(title)
				else:
					this.write_new_title('freeNews', new_titles)
					return new_titles
			page += 1

		this.write_new_title('freeNews', new_titles)
		return new_titles

	def appleNews(this):

		root_url = 'https://tw.appledaily.com'
		url = 'https://tw.appledaily.com/pf/api/v3/content/fetch/query-feed?query=%7B%22feedOffset%22%3A0%2C%22feedQuery%22%3A%22type%253Astory%2520AND%2520taxonomy.primary_section._id%253A%252F%255C%252Frealtime.*%252F%22%2C%22feedSize%22%3A%22100%22%2C%22sort%22%3A%22display_date%3Adesc%22%7D&filter=%7B_id%2Ccontent_elements%7B_id%2Ccanonical_url%2Ccreated_date%2Cdisplay_date%2Cheadlines%7Bbasic%7D%2Clast_updated_date%2Cpromo_items%7Bbasic%7B_id%2Ccaption%2Ccreated_date%2Cheight%2Clast_updated_date%2Cpromo_image%7Burl%7D%2Ctype%2Curl%2Cversion%2Cwidth%7D%2Ccanonical_website%2Ccredits%2Cdisplay_date%2Cfirst_publish_date%2Clocation%2Cpublish_date%2Crelated_content%2Csubtype%7D%2Crevision%2Csource%7Badditional_properties%2Cname%2Csource_id%2Csource_type%2Csystem%7D%2Ctaxonomy%7Bprimary_section%7B_id%2Cpath%7D%2Ctags%7Btext%7D%7D%2Ctype%2Cversion%2Cwebsite%2Cwebsite_url%7D%2Ccount%2Ctype%2Cversion%7D&d=136&_website=tw-appledaily'
		pre_titles = this.read_previous('appleNews')
		new_titles = {}

		elements = requests.get(url).json()['content_elements']
		for ele in elements:
			title = ele['headlines']['basic'].replace('\u3000','')
			link = root_url + ele['website_url']

			if title not in pre_titles:
				new_titles[title] = link
				print(title)
			else:
				this.write_new_title('appleNews', new_titles)
				return new_titles

		this.write_new_title('appleNews', new_titles)
		return new_titles

	def execute(this):
		this.chinaNews()
		this.formosa()
		this.threeStand()
		this.unionNews()
		this.freeNews()
		this.appleNews()

# nc = NewsCrawler()
# nc.execute()
# title = nc.chinaNews()
# title = nc.formosa()
# title = nc.threeStand()
# title = nc.unionNews()
# title = nc.freeNews()
# title = nc.appleNews()


#! /usr/bin/env python
# -*- coding: utf-8 -*-
from django.conf.urls import url, include
from appconf import database, product, project, appowner, api

urlpatterns = [
    url(r'^appowneradd/$', appowner.appowner_add, name='appowner_add'),
    url(r'^appowneraddmini/$', appowner.appowner_add_mini, name='appowner_add_mini'),
    url(r'^appowner/$', appowner.appowner_list, name='appowner_list'),
    url(r'^appowneredit/(?P<appowner_id>\d+)/$', appowner.appowner_edit, name='appowner_edit'),
    url(r'^appownerdel/$', appowner.appowner_del, name='appowner_del'),
    url(r'^productadd/$', product.product_add, name='product_add'),
    url(r'^product/$', product.product_list, name='product_list'),
    url(r'^productplist/(?P<product_id>\d+)/$', product.project_list, name='product_project_list'),
    url(r'^productedit/(?P<product_id>\d+)/$', product.product_edit, name='product_edit'),
    url(r'^productdel/$', product.product_del, name='product_del'),
    url(r'^projectadd/$', project.project_add, name='project_add'),
    url(r'^projectaddmini/$', project.project_add_mini, name='project_add_mini'),
    url(r'^project/$', project.project_list, name='project_list'),
    url(r'^projectedit/(?P<project_id>\d+)/$', project.project_edit, name='project_edit'),
    url(r'^projectdel/$', project.project_del, name='project_del'),
    url(r'^projectoffline/$', project.project_offline, name='project_offline'),
    url(r'^projectexport/$', project.project_export, name='project_export'),
    url(r'^projectdetail/(?P<project_id>\d+)/$', project.project_detail, name='project_detail'),
    url(r'^databaseadd/$', database.database_add, name='database_add'),
    url(r'^database/$', database.database_list, name='database_list'),
    url(r'^databaseedit/(?P<database_id>\d+)/$', database.database_edit, name='database_edit'),
    url(r'^databasedel/$', database.database_del, name='database_del'),
    url(r'^api/project/$', api.api_project_list.as_view(), name='api_project_list'),
    url(r'^api/projectproduct/$', api.api_project_product.as_view(), name='api_project_product'),
]


import re

from ui.dialogs_ui import invalidEmailError, invalidCredentials, invalidStudentNumError
from utils.helpers import DB_CONNECT, DB_CURSOR

def isNotEmpty(*args):
    for i in args:
        if i == "":
            return False
    return True

def isInDB(studentNumber):
    DB_CURSOR.execute(
        f"""SELECT COUNT(1) FROM student
            WHERE student_number = '{studentNumber}'
        """
    )

    res = DB_CURSOR.fetchall()[0][0]

    return True if res == 1 else False

def noLoan(studentNumber):
    DB_CURSOR.execute(
        f"""SELECT COUNT(1) FROM loan
            WHERE student_number = '{studentNumber}'
        """
    )

    res = DB_CURSOR.fetchall()[0][0]

    DB_CONNECT.commit()

    return True if not res else False

def validCredentials(studentNumber, *args):
    DB_CURSOR.execute(
        f"""SELECT COUNT(1) FROM student
            WHERE student_number = '{studentNumber}'
            AND name = '{args[0]}'
            AND email = '{args[1]}'
            AND passwd = '{args[2]}'
            AND college = '{args[3]}'
            AND course = '{args[4]}'
        """
    )

    res = DB_CURSOR.fetchall()[0][0]

    DB_CONNECT.commit()

    if res:
        return True
    invalidCredentials().exec()
    return False

def gwaAccepted(gwa):
    if 1.75 >= gwa >= 1.0:
        return True
    return False

def isFloat(userInput):
    try:
        float(userInput)
    except ValueError:
        return False
    return True

def isValidEmail(email):
    regex = r"\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b"
    if re.fullmatch(regex, email):
        return True
    invalidEmailError().exec()
    return False

def isValidStudentNum(studentNumber):
    regex = r"20\d{2}-\d{5}-MN-0"
    if re.fullmatch(regex, studentNumber):
        return True
    invalidStudentNumError().exec()
    return False


from django.conf.urls import url
from . import views

urlpatterns = [
    url(r'^$', views.post_list),
    url(r'home$', views.home),
    url(r'board$', views.board),
    url(r'cities$', views.cities),
    url(r'orgs$', views.orgs),
    ]


"""Testing Event Container.

Author: Yuhuang Hu
Email : duguyue100@gmail.com
"""
from __future__ import print_function

import cv2
import numpy as np

from pyaer import libcaer
from pyaer.davis import DAVIS

from timer import Timer

device = DAVIS(noise_filter=True)

print("Device ID:", device.device_id)
if device.device_is_master:
    print("Device is master.")
else:
    print("Device is slave.")
print("Device Serial Number:", device.device_serial_number)
print("Device String:", device.device_string)
print("Device USB bus Number:", device.device_usb_bus_number)
print("Device USB device address:", device.device_usb_device_address)
print("Device size X:", device.dvs_size_X)
print("Device size Y:", device.dvs_size_Y)
print("Logic Version:", device.logic_version)
print("Background Activity Filter:",
      device.dvs_has_background_activity_filter)
print("Color Filter", device.aps_color_filter, type(device.aps_color_filter))
print(device.aps_color_filter == 1)

device.start_data_stream()
# setting bias after data stream started
device.set_bias_from_json("./scripts/configs/davis346_config.json")

clip_value = 3
histrange = [(0, v) for v in (260, 346)]

def get_event(device):
    data = device.get_event()

    return data

num_packet_before_disable = 1000

while True:
    try:
        with Timer("container_test", show_hist=True):
            event_container = device.get_event_container()

        if event_container is not None:
            print("Duration (s): {}, Num Events: {}, Event Rate (ev/s): {}"
                  .format(
                    event_container.pol_event_duration,
                    event_container.num_pol_events,
                    event_container.pol_event_rate))
            print("Signal Rate (ev/s): {}, Noise Rate (ev/s): {}"
                  .format(
                    event_container.valid_pol_events_rate,
                    event_container.invalid_pol_events_rate))

            #  print("Number of events:", num_pol_event, "Number of Frames:",
            #        frames.shape, "Exposure:",
            #        device.get_config(
            #            libcaer.DAVIS_CONFIG_APS,
            #            libcaer.DAVIS_CONFIG_APS_EXPOSURE),
            #        "Autoexposure:", device.get_config(
            #            libcaer.DAVIS_CONFIG_APS,
            #            libcaer.DAVIS_CONFIG_APS_AUTOEXPOSURE))
        else:
            pass

    except KeyboardInterrupt:
        device.shutdown()
        break


import scapy

import scapy_layer_gtp
from scapy.all import *
from scapy.utils import rdpcap

import threading
import time
import argparse

parser = argparse.ArgumentParser(description='Process some integers.')
parser.add_argument('--delay', type=float,default=0.1,
                    help='an integer for the accumulator')

args = parser.parse_args()
print(args.delay)

pkts=rdpcap("enb_pcap.pcap")  # could be used like this rdpcap("filename",500) fetches first 500 pkts

#https://stackoverflow.com/questions/33740583/writing-to-a-pcap-with-scapy

def write(pkt):
    wrpcap('filtered.pcap', pkt, append=True)  #appends packet to output file

i=0

for pkt in pkts:
     #pkt[Ether].src= new_src_mac  # i.e new_src_mac="00:11:22:33:44:55"
     #pkt[Ether].dst= new_dst_mac
     #pkt[IP].src= new_src_ip # i.e new_src_ip="255.255.255.255"
     #pkt[IP].dst= new_dst_ip
     #pkt.show()

        if pkt[IP].src == "10.80.95.10" and pkt.haslayer(UDP):
            if i%2==0:
                pkt["GTPHeader"].TEID=1  #change TEID
            else:
                pkt["GTPHeader"].TEID=2  #change TEID
          #sendp(pkt, count=1, iface="vm1") #sending packet at layer 2
            i+=1

def thread_function(pkt):
    sendp(pkt, iface="vm1") #sending packet at layer 2

while True:
    for pkt in pkts:
        #sendp(pkt, iface="vm1", loop=1, inter=0.5) #sending packet at layer 2
        x = threading.Thread(target=thread_function, args=(pkt,))
        x.start()
        time.sleep(args.delay)

    # x.join()


# zc-cris

# demo1：将列表中的所有人名的末尾加上_sb
names = ['cris', 'alex', 'james']
i = map(lambda x: x + "_sb", names)  # map 返回的结果就是迭代器
for a in i:
    print(a)

# demo02：用filter将列表中的所有偶数计算出来
numbers = [1, 2, 3, 4]
iterator = filter(lambda x: x % 2 == 0, numbers)
# iterator = filter(lambda x: True if x %2 ==0 else False, numbers)
print(list(iterator))  # [2, 4]

# demo03 如下，计算购买每支股票的总价，以及过滤出单价大于1000的股票
stocks = [
    {'name': 'apple', 'shares': 100, 'price': 1000.23},
    {'name': 'samsung', 'shares': 23, 'price': 134.23},
    {'name': '长生制药', 'shares': 1, 'price': 0.23},
    {'name': '小米', 'shares': 500, 'price': 1123.23},

]
# 计算每支股票的总价
map1 = map(lambda x: {x['name']: x.get('shares') * x.get('price')}, stocks)
print(list(map1))  # [{'apple': 100023.0}, {'samsung': 3087.29}, {'长生制药': 0.23}, {'小米': 561615.0}]

# 计算所有股票的总价
total_price = 0
for i in stocks:
    total_price += i.get('shares') * i.get('price')
print(total_price)  # 664725.52

filter1 = filter(lambda x: x.get('price') > 1000, stocks)
print(list(
    filter1))  # [{'name': 'apple', 'shares': 100, 'price': 1000.23}, {'name': '小米', 'shares': 500, 'price': 1123.23}]

# # python 中的手动分页demo：divmod(被除数，除数)
# with open("file", encoding='utf-8') as file:
#     content = file.readlines()
#
# page_num = input("每页显示5条数据，请输入你要查看的页码：")
# page_num = int(page_num)
# total_pages, mod = divmod(len(content), 5)
# # 如果有余数，说明最大页数应该在商的基础上加1
# if mod: total_pages += 1
#
# if page_num > total_pages:
#     print("输入的数字大于总页数：{}".format(total_pages))
# elif page_num < 1:
#     print("请输入正确的页码")
# elif page_num == total_pages and mod:  # 如果用户查询最后一页，且最后一页内容数量不足page_num
#     for i in range(mod):
#         print(content[(page_num - 1) * 5 + i], end='')
# else:
#     for i in range(5):
#         print(content[(page_num - 1) * 5 + i], end='')


import sqlite3
import pandas as pd

def main():
    df = pd.read_csv('pokemon.csv')

    df = df[['pokedex_number', 'name', 'generation', 'type1', 'type2', 'is_legendary',
             'hp', 'attack', 'defense', 'sp_attack', 'sp_defense', 'speed']]
    df = df.set_index('name')

    with sqlite3.connect('pokemons.db') as conn:
        df.to_sql('pokemons', conn)

if __name__ == '__main__':
    main();


"""Write a function that takes an ordered list of numbers (a list where the elements are in order from smallest to
largest) and another number. The function decides whether or not the given number is inside the list and returns (
then prints) an appropriate boolean.

Extras:

    Use binary search.
"""
from time import time

def busqueda_normal(numero_a_buscar, lista_elementos):
    """Función que toma un número ingresado por el usuario y lo busca en una lista ordenada de elementos."""
    busqueda = [num for num in lista_elementos if num == numero_a_buscar]
    if busqueda:
        return print(f"{numero_a_buscar} si esta en la lista")
    else:
        return print(f"{numero_a_buscar} no esta en la lista")

def busqueda_binaria(numero_a_buscar, lista_elementos):
    """Función que busca un número ingresado en una lista ordenada a través de búsqueda binaria con un ciclo."""
    izquierda, derecha = (
        0,
        len(lista_elementos) - 1,
    )  # Establecemos los indices para cada lado del arreglo.
    while izquierda <= derecha:
        indice_del_elemento_del_medio = (izquierda + derecha) // 2
        elemento_medio = lista_elementos[indice_del_elemento_del_medio]
        if elemento_medio == numero_a_buscar:
            return print(
                f"El número {numero_a_buscar} se encuentra en la lista en el índice: {indice_del_elemento_del_medio}"
            )
        if numero_a_buscar < elemento_medio:
            derecha = indice_del_elemento_del_medio - 1
        else:
            izquierda = indice_del_elemento_del_medio + 1
    # Si se sale del ciclo, significa que no existe el valor
    return print(f"{numero_a_buscar} no esta en la lista")

if __name__ == "__main__":
    numero = int(input("Ingrese número a buscar en la lista: "))
    lista = [1, 2, 3, 10, 30, 50, 77, 90, 100]

    start_time = time()
    busqueda_normal(numero, lista)
    elapsed_time = time() - start_time
    print("Tiempo transcurrido búsqueda normal: %.10f segundos\n" % elapsed_time)

    start_time = time()
    busqueda_binaria(numero, lista)
    elapsed_time = time() - start_time
    print("Tiempo transcurrido búsqueda binaria: %.10f segundos" % elapsed_time)


#!/usr/bin/python3
import hashlib
import string
import itertools
import sys

hash = "CD04302CBBD2E0EB259F53FAC7C57EE2"

alphabet = string.printable

for length in range(1, 8): # the string is REALLY short
	print("Len: {}".format(length))
	for i in itertools.product(alphabet, repeat=length):
		s = ''.join(i)
		for cnt in range(10):
			h = hashlib.new('md5')
			h.update(s.encode())
			s = h.hexdigest().upper()
		if s == hash:
			print("Found string: {}".format(''.join(i)))
			sys.exit(0)

2019/Redpwn CTF 2019/Super Hash/solve2.py


import cv2
import numpy as np
import matplotlib.pyplot as plt
def calculate_similarity(image_path1, image_path2):
    # 加载图片
    image1 = cv2.imread(image_path1)
    image2 = cv2.imread(image_path2)

    # 确保图片大小相同
    if image1.shape != image2.shape:
        print("两张图片的大小不同，无法比较。")
        return
    else:
        # 转换为灰度图
        image1 = cv2.cvtColor(image1, cv2.COLOR_BGR2GRAY)
        image2 = cv2.cvtColor(image2, cv2.COLOR_BGR2GRAY)

        # 计算图片的直方图
        hist1 = cv2.calcHist([image1], [0], None, [256], [0,256])
        hist2 = cv2.calcHist([image2], [0], None, [256], [0,256])

        # 通过compareHist函数来计算相似度
        similarity = cv2.compareHist(hist1, hist2, cv2.HISTCMP_BHATTACHARYYA)

        return similarity

def template_match(image_path, template_path):
    image = cv2.imread(image_path, 0)
    template = cv2.imread(template_path, 0)
    w, h = template.shape[::-1]

    res = cv2.matchTemplate(image, template, cv2.TM_CCOEFF_NORMED)
    min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res)

    top_left = max_loc
    bottom_right = (top_left[0] + w, top_left[1] + h)
    cv2.rectangle(image,top_left, bottom_right, 255, 2)

    plt.subplot(121),plt.imshow(res,cmap = 'gray')
    plt.title('Matching Result'), plt.xticks([]), plt.yticks([])
    plt.subplot(122),plt.imshow(image,cmap = 'gray')
    plt.title('Detected Point'), plt.xticks([]), plt.yticks([])
    plt.suptitle('cv2.TM_CCOEFF_NORMED')
    plt.show()

def orb_sim(img1, img2):
    # 初始化ORB检测器
    orb = cv2.ORB_create()

    # 检测关键点和计算描述符
    kp1, des1 = orb.detectAndCompute(img1, None)
    kp2, des2 = orb.detectAndCompute(img2, None)

    # 定义匹配器并进行匹配
    bf = cv2.BFMatcher(cv2.NORM_HAMMING, crossCheck=True)
    matches = bf.match(des1, des2)

    # 按距离排序
    matches = sorted(matches, key=lambda x: x.distance)

    # 返回匹配特征点的数量
    return len(matches)

# 测试
image1_path = 'C:/Users/14692/Desktop/target.png'
image2_path = 'C:/Users/14692/Desktop/test_0.jpg'
img1 = cv2.imread(image1_path, 0)  # 原图像
img2 = cv2.imread(image2_path, 0)  # 旋转后的图像
similarity = calculate_similarity(image1_path, image2_path)
print('图片的0相似度是：', similarity)
print(orb_sim(img1, img2))
image2_path = 'C:/Users/14692/Desktop/test_1.jpg'
img2 = cv2.imread(image2_path, 0)  # 旋转后的图像
similarity = calculate_similarity(image1_path, image2_path)
print('图片的1相似度是：', similarity)
print(orb_sim(img1, img2))
# 使用方法：

# template_match(image2_path, image1_path)


import logging
import time
from threading import Thread, Lock
from typing import List, Dict

from mill.job import Job, EmptyJob, empty_job, execute

class JobRunner(Thread):
    _scheduled_jobs: List[Job]
    _active_job: Job
    _abort: bool
    _lock: Lock
    _run_status: str

    def __init__(self):
        Thread.__init__(self)
        self._scheduled_jobs = []
        self._abort = False
        self._active_job = EmptyJob()
        self._run_status = "Idle"
        self._lock = Lock()

    def abort_active(self) -> None:
        with self._lock:
            self._active_job.cancel()

    def abort_schedule(self) -> None:
        with self._lock:
            self._scheduled_jobs = []

    def get_state(self) -> Dict:
        with self._lock:
            scheduled_jobs = _get_job_list_status(self._scheduled_jobs)
            active_job = self._active_job.serialize()
        state = {"run_status": self._run_status, "schedule": scheduled_jobs, "active_job": active_job}
        return state

    def add_job_to_queue(self, job: Job):
        with self._lock:
            self._scheduled_jobs.append(job)

    def run(self):
        while True:
            time.sleep(1)
            job = self._pop_job()
            self._set_active_job(job)
            if job != empty_job:
                execute(job)

    def _set_active_job(self, job: Job):
        with self._lock:
            self._active_job = job
            self._run_status = "Idle" if job == empty_job else "Running"

    def _pop_job(self) -> Job:
        with self._lock:
            if self._scheduled_jobs:
                return self._scheduled_jobs.pop(0)
            else:
                return empty_job

def _get_job_list_status(job_list: List[Job]) -> List[Dict]:
    job_status = [job.serialize() for job in job_list]
    return job_status


import numpy as np
import gym
import tensorflow as tf

tf.GraphKeys.VARIABLES = tf.GraphKeys.GLOBAL_VARIABLES

MEMORY_CAPACITY = 10000
BATCH_SIZE = 32
D = 80 * 80
RENDER = False
is_train = False

class DDPG(object):
    def __init__(self, a_dim, s_dim, lr_a, lr_c, gamma, tau):
        self.memory = np.zeros((MEMORY_CAPACITY, s_dim * 2 + 2), dtype=np.float32)
        self.pointer = 0
        self.lr_actor = lr_a     # learning rate for actor
        self.lr_critic = lr_c    # learning rate for critic
        self.gamma = gamma       # reward discount
        self.tau = tau           # soft replacement
        self.a_replace_counter, self.c_replace_counter = 0, 0

        self.a_dim, self.s_dim = a_dim, s_dim
        self.S = tf.placeholder(tf.float32, [None, s_dim], 's')
        self.S_ = tf.placeholder(tf.float32, [None, s_dim], 's_')
        self.R = tf.placeholder(tf.float32, [None, 1], 'r')

        with tf.variable_scope('Actor'):
            self.a = self._build_a(self.S, scope='eval', trainable=True)
            a_ = self._build_a(self.S_, scope='target', trainable=False)
        with tf.variable_scope('Critic'):
            # assign self.a = a in memory when calculating q for td_error,
            # otherwise the self.a is from Actor when updating Actor
            q = self._build_c(self.S, self.a, scope='eval', trainable=True)
            q_ = self._build_c(self.S_, a_, scope='target', trainable=False)

        # networks parameters
        self.ae_params = tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES, scope='Actor/eval')
        self.at_params = tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES, scope='Actor/target')
        self.ce_params = tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES, scope='Critic/eval')
        self.ct_params = tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES, scope='Critic/target')

        # target net replacement
        self.soft_replace = [
            [tf.assign(ta, (1 - self.tau) * ta + self.tau * ea), tf.assign(tc, (1 - self.tau) * tc + self.tau * ec)]
            for ta, ea, tc, ec in zip(self.at_params, self.ae_params, self.ct_params, self.ce_params)]

        q_target = self.R + self.gamma * q_
        # in the feed_dic for the td_error, the self.a should change to actions in memory
        td_error = tf.losses.mean_squared_error(labels=q_target, predictions=q)
        self.ctrain = tf.train.AdamOptimizer(self.lr_critic).minimize(td_error, var_list=self.ce_params)

        a_loss = - tf.reduce_mean(q)  # maximize the q
        self.atrain = tf.train.AdamOptimizer(self.lr_actor).minimize(a_loss, var_list=self.ae_params)

        self.sess = tf.Session()
        self.sess.run(tf.global_variables_initializer())
        self.saver = tf.train.Saver()
        self.restore_file = tf.train.latest_checkpoint('ckpt/pong_deterministic_pg/')

    def choose_action(self, s):
        return self.sess.run(self.a, {self.S: s[np.newaxis, :]})[0]

    def learn(self):
        # soft target replacement
        self.sess.run(self.soft_replace)

        indices = np.random.choice(MEMORY_CAPACITY, size=BATCH_SIZE)
        bt = self.memory[indices, :]
        bs = bt[:, :self.s_dim]
        ba = bt[:, self.s_dim: self.s_dim + 1]
        br = bt[:, -self.s_dim - 1: -self.s_dim]
        bs_ = bt[:, -self.s_dim:]

        self.sess.run(self.atrain, {self.S: bs})
        self.sess.run(self.ctrain, {self.S: bs, self.a: ba, self.R: br, self.S_: bs_})

    def store_transition(self, s, a, r, s_):
        transition = np.hstack((s, a, [r], s_))
        index = self.pointer % MEMORY_CAPACITY  # replace the old memory with new memory
        self.memory[index, :] = transition
        self.pointer += 1

    def _build_a(self, s, scope, trainable):
        with tf.variable_scope(scope):
            net = tf.layers.dense(inputs=s, units=100, activation=tf.nn.relu,
                                  kernel_initializer=tf.random_normal_initializer(mean=0, stddev=0.3),
                                  bias_initializer=tf.constant_initializer(0.1), name='l1', trainable=trainable)
            a = tf.layers.dense(inputs=net, units=1, activation=tf.nn.tanh,
                                kernel_initializer=tf.random_normal_initializer(mean=0, stddev=0.3),
                                bias_initializer=tf.constant_initializer(0.1), name='a', trainable=trainable)
            return a

    def _build_c(self, s, a, scope, trainable):
        with tf.variable_scope(scope):
            n_l1 = 100
            w1_s = tf.get_variable('w1_s', [self.s_dim, n_l1], trainable=trainable)
            w1_a = tf.get_variable('w1_a', [1, n_l1], trainable=trainable)
            b1 = tf.get_variable('b1', [1, n_l1], trainable=trainable)
            net = tf.nn.relu(tf.matmul(s, w1_s) + tf.matmul(a, w1_a) + b1)
            return tf.layers.dense(net, 1, trainable=trainable)  # Q(s,a)

env = gym.make('Pong-v0')
env = env.unwrapped
env.seed(1)

s_dim = env.observation_space.shape[0]
a_dim = env.action_space.n
print(s_dim)
print(a_dim)

ddpg = DDPG(a_dim, D, 0.01, 0.02, 0.98, 0.01)

if is_train:
    model_file = ddpg.restore_file
    ddpg.saver.restore(ddpg.sess, model_file)

def pre_process(I):
    I = I[35:195]
    I = I[::2, ::2, 0]
    I[I == 144] = 0
    I[I == 109] = 0
    I[I != 0] = 1
    return I.astype(np.float).ravel()

var = 3  # control exploration
for i in range(3000):
    observation = env.reset()
    ep_reward = 0
    pre_state = None
    for j in range(200):
        # if RENDER:
        #    env.render()

        # Add exploration noise
        cur_state = pre_process(observation)
        x = cur_state - pre_state if pre_state is not None else np.zeros(D)
        pre_state = cur_state

        act = ddpg.choose_action(x)
        act = np.clip(np.random.normal(act, var), -2, 3)  # add randomness to action selection for exploration
        action_step = int(act)
        action_step += 2
        s_, r, done, info = env.step(action_step)

        x_ = pre_process(s_)
        ddpg.store_transition(x, action_step, r, x_)

        if ddpg.pointer > MEMORY_CAPACITY:
            var *= .9995  # decay the action randomness
            ddpg.learn()

        observation = s_
        ep_reward += r

        if j == 10:
            print("act=%d, r=%.2f" % (action_step, r))
        elif j == 50:
            print("act=%d, r=%.2f" % (action_step, r))
        elif j == 150:
            print("act=%d, r=%.2f" % (action_step, r))

        if j == 199:
            print('Episode:', i, ' Reward: %.2f' % ep_reward, 'Explore: %.2f' % var)
            print('**************************************')
            ddpg.saver.save(ddpg.sess, 'ckpt/pong_deterministic_pg/ddpg.ckpt')
            # if ep_reward > -30:
            #    RENDER = True
            break


def func(*args, **kwargs):
    print(args)
    nome = kwargs.get('nome')
    idade = kwargs.get('idade')
    if idade:
        print(nome, idade)
    else:
        print(nome, 'Idade não informada')

func(1, 2, 3, 4, 5, nome='Ivander', sobrenome='Salvador')
# (1, 2, 3, 4, 5)
# {'nome': 'Ivander', 'sobrenome': 'Salvador'}
# Ivander


import math
# 이 문제를 무려 2번이나 틀렸음. 굉장히 간단하다고 생각했는데 실수만 2번 했다.
# 첫 번째 실수는 단순히 측을 계산할 때 나머지로 계산 한 것 딱 나눠 떨어지는 경우 최고층을 고려해야했음
# 두 번째 실수는 math.ceil()을 사용하는 데 바보같이 n//h를 사용한 것 몫에 올림을해도 소수점이 없는데 자주 하는 실수인 듯

t = int(input())
h_list = []
w_list = []
n_list = []
for i in range(t):
    h, w, n = map(int, list(input().split()))
    h_list.append(h)
    w_list.append(w)
    n_list.append(n)

for i in range(t):
    h = h_list[i]
    w = w_list[i]
    n = n_list[i]
    floor = n % h
    if floor == 0:
        floor = h
    number = math.ceil(n/h)
    print(floor, end='')
    print(format(number, '02'))


import pandas as pd
import numpy as np
import torch
from tqdm import tqdm
from collections import Counter
import matplotlib.pyplot as plt

from iglm import IgLM

def get_hidden(
    iglm_model,
    x,
    chain_token,
    species_token,
):
    h = []
    for sequence in x:
        sequence = list(sequence)
        token_seq = [chain_token, species_token] + sequence
        token_seq += [iglm_model.tokenizer.sep_token]

        token_seq = torch.Tensor([
            iglm_model.tokenizer.convert_tokens_to_ids(token_seq)
        ]).int().to(iglm_model.device)

        with torch.no_grad():
            out = iglm_model.model(
                token_seq,
                output_hidden_states=True
            )
        h.append(out.hidden_states[-1].mean(1))

    h = torch.cat(h, dim=0)
    return h

def energy_function(
    iglm_model,
    guidance_model,
    x,
    chain_token,
    species_token,
    fitness_weight=0.1
):
    likelihoods = []
    for seq in x:
        likelihoods.append(
            iglm_model.log_likelihood(
                seq,
                chain_token=chain_token,
                species_token=species_token,
            )
        )
    likelihoods = np.array(likelihoods)

    fitness = guidance_model(
        get_hidden(
            iglm_model,
            x,
            chain_token,
            species_token,
        )
    ).squeeze(-1)
    fitness = fitness.detach().cpu().numpy()

    return likelihoods + fitness_weight * fitness

def mutate(x, vocab):
    for _ in range(1):
        pos = [np.random.randint(len(seq)) for seq in x]
        vals = [vocab[np.random.randint(len(vocab))] for _ in x]
        x_mut = []
        for seq, i, val in zip(x, pos, vals):
            x_mut.append(
                seq[:i] + val + seq[i+1:]
            )
        x = x_mut
    return x_mut

def mcmc_step(
    iglm_model,
    guidance_model,
    x,
    vocab,
    chain_token,
    species_token,
    T=1,
    fitness_weight=0.1
):
    x_mut = mutate(x, vocab)

    E = energy_function(
        iglm_model,
        guidance_model,
        x,
        chain_token,
        species_token,
        fitness_weight=fitness_weight
    )

    E_mut = energy_function(
        iglm_model,
        guidance_model,
        x_mut,
        chain_token,
        species_token,
        fitness_weight=fitness_weight
    )

    accept_p = np.minimum(1, np.exp(-(E - E_mut) / T))
    accept = np.random.rand(len(x)) < accept_p
    # print(np.mean(np.minimum(1-accept_p,accept_p)))

    x_new = [s_new if a else s for a, s_new, s in zip(accept, x_mut, x)]
    E_new = [e_new if a else e for a, e_new, e in zip(accept, E_mut, E)]

    return x_new, E_new

def mcmc_sample(
    iglm_model,
    guidance_model,
    vocab,
    chain_token,
    species_token,
    num_steps=2000,
    fitness_weight=0.1
):
    x = iglm_model.generate(
        chain_token=chain_token,
        species_token=species_token,
        num_to_generate=10,
    )
    x = [''.join(np.random.choice(vocab, size=len(s))) for s in x]

    E_traj, x_traj = [], []
    for i in tqdm(list(range(num_steps))):
        new_x, E_new = mcmc_step(
            iglm_model,
            guidance_model,
            x,
            vocab,
            chain_token,
            species_token,
            T=1e-2 * (0.5) ** (i // 2000),#8-(i // 200),
            fitness_weight=fitness_weight,
        )

        # for s, s_mut in zip(x, new_x):
        #     print(s == s_mut)

        x = new_x

        E_traj.append(E_new)
        x_traj.append(new_x)

    E_traj = np.array(E_traj)
    return x_traj, E_traj

def main():
    iglm_model = IgLM()
    vocab = [k for k in iglm_model.tokenizer.vocab.keys() if '[' not in k]
    guidance_model = None

    iglm_model.model.cuda()
    guidance_model.cuda()

    fitness_weight=0.1
    num_steps = 20000

    lv_traj, lv_E = mcmc_sample(
        iglm_model,
        vocab,
        "[LIGHT]",
        "[HUMAN]",
        num_steps=num_steps,
        fitness_weight=fitness_weight
    )
    plt.plot(-lv_E)
    plt.show()

    hv_traj, hv_E = mcmc_sample(
        iglm_model,
        vocab,
        "[HEAVY]",
        "[HUMAN]",
        num_steps=num_steps,
        fitness_weight=fitness_weight
    )
    plt.plot(-hv_E)
    plt.show()

    for i in range(10):
        lvs = [x[i] for x in lv_traj]
        e1s = [x[i] for x in lv_E]
        hvs = [x[i] for x in hv_traj]
        e2s = [x[i] for x in hv_E]
        dicts = []
        for lv, e1, hv, e2 in zip(lvs, e1s, hvs, e2s):
            dicts.append({
                'lv': lv,
                'e_lv': e1,
                'hv': hv,
                'e_hv': e2,
            })
        df = pd.DataFrame(dicts)
        df.to_csv(f"mcmc_i={i}_fitness={fitness_weight}.csv", index=False)

if __name__ == "__main__":
    main()


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

# Current package semantic version
__version__ = version = "0.1.0"

import os
import sys
import argparse
import csv
from scraper import Scraper

# Declare supported command-line flags
parser = argparse.ArgumentParser(description='Scrape all thread posts of a phpBB based forum', epilog='Report any issues to https://github.com/h2non/bbscraper/issues')
parser.add_argument('-u','--url', required=True, nargs=1, help='Full URL to the forum thread to scrape', type=str, default='http://www.oldclassiccar.co.uk/forum/phpbb/phpBB2/viewtopic.php?t=12591')
parser.add_argument('-f','--format', required=False, nargs=1, help='Output format', default='csv')
parser.add_argument('-v', '--version', action='version', version='bbscraper %s' % version)

# Parse command-line arguments
args = parser.parse_args()

# main is automatically executed when calling
# the script directly via Python interpreter
def main():
    # Collect target URL to scrape from CLI flag
    url = args.url[0]

    # Create the CSV serializer and writer (by default writes into stdout)
    writer = csv.writer(sys.stdout)

    try:
        Scraper(writer).scrape(url)
    except:
        print("Unexpected error:", sys.exc_info()[0])
        raise

# run main function if we're running as main module
if __name__ == "__main__":
    main()


import asyncio
import aiohttp
from aiohttp.web import WebSocketResponse
from gns3server.web.route import Route
from gns3server.compute.notification_manager import NotificationManager

import logging
log = logging.getLogger(__name__)

async def process_websocket(ws):
    """
    Process ping / pong and close message
    """
    try:
        await ws.receive()
    except aiohttp.WSServerHandshakeError:
        pass

class NotificationHandler:

    @Route.get(
        r"/notifications/ws",
        description="Send notifications using Websockets")
    async def notifications(request, response):
        notifications = NotificationManager.instance()
        ws = WebSocketResponse()
        await ws.prepare(request)

        request.app['websockets'].add(ws)
        asyncio.ensure_future(process_websocket(ws))
        log.info("New client has connected to compute WebSocket")
        try:
            with notifications.queue() as queue:
                while True:
                    notification = await queue.get_json(1)
                    if ws.closed:
                        break
                    await ws.send_str(notification)
        finally:
            log.info("Client has disconnected from compute WebSocket")
            if not ws.closed:
                await ws.close()
            request.app['websockets'].discard(ws)

        return ws