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

import os
import json
import  unittest
from datetime import datetime
from topogram.topogram import Topogram
from topogram.processors.nlp import NLP
from topogram.corpora.csv_file import CSVCorpus

# get a csv corpus
csv_path = os.path.join(os.getcwd(), "tests/sampleweibo.csv")
corpus = CSVCorpus(csv_path, origin="uid", time_pattern="%Y-%m-%d %H:%M:%S")

# get Chinese NLP
nlp = NLP("zh")
nlp.add_stopword("ukn")

class TestTopogram(unittest.TestCase):

    def setUp(self):
        """Create a topogram """
        self.topogram = Topogram(corpus=corpus, processors=[nlp])
        self.nlp = nlp
        self.corpus = corpus

    def test_init_instances_raise_errors(self):
        """ should only accept a Corpus instance and a list of processors"""
        self.assertRaises(TypeError, lambda : Topogram(1, [self.nlp]))
        self.assertRaises(TypeError, lambda : Topogram(self.corpus, 1))
        self.assertRaises(TypeError, lambda : Topogram(self.corpus, [1]))

    def test_init_store_values(self):
        """ should store corpus and processors"""
        self.assertEquals(self.topogram.corpus, self.corpus)
        self.assertEquals(self.topogram.processors[0], self.nlp)

    # def test_add_multiple_regexps(self):
    #     hashtagPattern=r"#([^#\s]+)#"
    #     self.topogram.add_citation_regexp([hashtagPattern, hashtagPattern])
    #     self.assertEquals(len(self.topogram.citation_regexps), 2)

    # def test_extract_citations(self):
    #     # add a citation pattern
    #     hashtagPattern=r"#([^#\s]+)#"
    #     self.topogram.add_citation_regexp(hashtagPattern)

    #     # extract citation
    #     citations = self.topogram.extract_citations("bonjour #yoyo# #yoyo# #yaya#")
    #     self.assertTrue(len(citations) == 3)

    # def test_ignore_citations(self):
    #     # add a citation pattern
    #     hashtagPattern=r"#([^#\s]+)#"
    #     self.topogram.add_citation_regexp(hashtagPattern)

    #     # extract citation
    #     self.topogram.add_citation_to_ignore("yaya")
    #     citations = self.topogram.extract_citations("bonjour #yoyo# #yoyo# #yaya#")
    #     self.assertTrue(len(citations) == 2)

    # def test_is_weighted_network(self):
    #     self.add_nx_data()
    #     print self.topogram.words.edges(data=True)
    #     self.assertTrue(self.topogram.words["a"]["b"]["weight"] == 2)
    #     self.assertTrue(self.topogram.citations["a"]["b"]["weight"] == 2)

    # def test_get_nodes_degree(self):
    #     self.add_nx_data()
    #     deg = self.topogram.get_nodes_degree(self.topogram.words)
    #     print len(deg)
    #     self.assertEquals(len(deg), 3)

    # def test_get_words(self):
    #     self.add_nx_data()
    #     w = self.topogram.get_words()
    #     self.assertEquals(w, self.topogram.words)

    # def test_get_average_degree_connectivity(self):
    #     self.add_nx_data()
    #     d = self.topogram.get_average_degree_connectivity(self.topogram.words)
    #     print d, type(d)
    #     self.assertTrue(type(d) is dict)
    #     self.assertTrue(type(d[1]) is float)

    # def test_export_to_d3_js(self):
    #     self.add_nx_data()
    #     d3_json = self.topogram.export_words_to_d3_js()
    #     print d3_json
    #     self.assertTrue( type(d3_json["nodes"]) is list)
    #     self.assertTrue( type(d3_json["links"]) is list)

    # def test_export_words_to_json(self):
    #     self.add_nx_data()
    #     words_json = self.topogram.export_words_to_json()
    #     print words_json
    #     self.assertTrue( type(words_json["nodes"]) is list)
    #     self.assertTrue( type(words_json["links"]) is list)

    # def test_load_words_from_json(self):
    #     self.add_nx_data()
    #     words_json = self.topogram.export_words_to_d3_js()

    #     topogram = Topogram(corpus, nlp)
    #     topogram.load_words_from_json(words_json)
    #     self.assertEquals(topogram.words.nodes(), self.topogram.words.nodes())
    #     self.assertEquals(topogram.words.edges(), self.topogram.words.edges())

    # def test_get_nodes(self):
    #     self.add_nx_data()
    #     self.assertEquals(self.topogram.get_nodes(self.topogram.words), self.topogram.words.nodes())

    # def test_get_node_network(self):
    #     self.add_nx_data()
    #     g = self.topogram.get_node_network(self.topogram.words)
    #     self.assertTrue(g.nodes() < self.topogram.words.nodes())

# class TestBasicTopogram(unittest.TestCase):

#     def setUp(self):

#         # create topogram
#         corpus = CSVCorpus(csv_path, source_column="uid", time_pattern="%Y-%m-%d %H:%M:%S")
#         corpus.validateCSV()
#         self.topogram = BasicTopogram(corpus, nlp)

#         self.topogram.add_citation_to_ignore("ukn")
#         self.topogram.add_citation_to_ignore("uid")

#         urlPattern=r"\b(([\w-]+://?|www[.])[^\s()<>]+(?:\([\w\d]+\)|([^\p{P}\s]|/)))"
#         hashtagPattern=r"#([^#\s]+)#"
#         self.topogram.nlp.add_stop_regexp(urlPattern)

#         # self.topogram.nlp.add_stopword(u' ')

#     def test_process(self):
#         self.topogram.process()
#         self.assertTrue(self.topogram.citations.order() != 0)

#     # def test_list_of_top_words(self):
#     #     self.topogram.process()
#     #     top_words = self.topogram.get_top_words(150)
#     #     self.assertTrue(len(top_words) == 2)
#     #     top_citations = self .topogram.get_top_citations(20)

#     #     print top_citations
#     #     self.assertTrue(len(top_citations) == 1)

#     def test_densities(self):
#         self.topogram.process()
#         self.assertTrue(self.topogram.get_words_density() < 1)
#         self.assertTrue(self.topogram.get_citations_density() < 1)

# # class TestPreProcess(unittest.TestCase):
# #     def setUp(self):

# #         # create topogram
# #         corpus = CSVCorpus(csv_path, source_column="uid", time_pattern="%Y-%m-%d %H:%M:%S")
# #         corpus.validate()
# #         # self.topogram = BasicTopogram(corpus, nlp)
# #         self.preproc = NLPPreProcess(corpus=corpus, nlp=nlp)

# #     def test_process(self):
# #         rows = []
# #         for row in  self.preproc.process() :
# #             print len(row["text_column"] )
# #             self.assertTrue(len(row["text_column"]) !=  0)
# #             self.assertTrue(type(row["text_column"]) is str)

# #     def test_preprocess_return_rows(self):
# #         rows = [row for row in  self.preproc.process() ]
# #         self.assertEquals(len(rows), 120)

if __name__ == '__main__':
    unittest.main()


from odoo import models, api, fields

class RestaurantOrder(models.Model):
    _inherit='restaurant.order'

    invoice_id=fields.Many2one('customer.invoice', 'Invoice')

    def create_invoice(self):
        vals={}
        invoice_obj=self.env['customer.invoice']
        vals={'customer_name':self.customer,
                'total_amount':self.order_total}
        invoice=invoice_obj.create(vals)
        self.write({'invoice_id':invoice.id})


import sys
import math

from PyQt5.QtCore import pyqtSignal, QPoint, QSize, Qt
from PyQt5.QtGui import QColor
from PyQt5.QtWidgets import (QApplication, QHBoxLayout, QOpenGLWidget, QSlider,
                             QWidget)
from Widgets.openGL_widgets.CubicGLContext import CubicGLContext
from Widgets.openGL_widgets.ArrowGLContext import ArrowGLContext
import OpenGL.GL as gl
import OpenGL.GLU as glu
import os
from processing.multiprocessing_parse import MultiprocessingParse

class Window(QWidget):

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

        # directory = r"D:\Dokumenty\VISM\examples\730uA"
        directory = r"D:\Dokumenty\VISM\examples\0200nm"
        files = os.listdir(directory)
        i = 0
        filename = files[i]
        while(not (filename.endswith(".omf") or filename.endswith(".ovf"))):
            i += 1
            filename = files[i]
        # rawVectorData, header, _, stages, _ = \
        #                     MultiprocessingParse.readFolder(directory)
        rawVectorData, header = MultiprocessingParse.readFile(os.path.join(directory, filename))
        """
        new cross color
        user vector 1st
        positive vector 2nd
        negative vector 3rd
        """
        self.options = [True, 5, 'all', 2,
        [[1.0, 0, 0], [1.0, 0, 0], [0, 0, 1.0]], 'Standard', False, 12]
        data_dict = {
                        "color_vectors" : rawVectorData,
                        "file_header": header,
                        "iterations": 1,
                        "averaging": 2,
                        "options": self.options,
                        "current_state": 0,
                        "geom": (800, 400),
        }
        self.glWidget = ArrowGLContext(data_dict, self)
        # self.glWidget = CubicGLContext(data_dict, self)
        mainLayout = QHBoxLayout()
        mainLayout.addWidget(self.glWidget)
        self.setLayout(mainLayout)
        self.setWindowTitle("Hello GL")

if __name__ == '__main__':

    app = QApplication(sys.argv)
    window = Window()
    window.resize(1200, 800)
    window.show()
    sys.exit(app.exec_())


import clr
import sys
import json
import os
import ctypes
import codecs
import time

ScriptName = "Raffle Minigame"
Website = "http://www.github.com/Bare7a/Streamlabs-Chatbot-Scripts"
Description = "Raffle Minigame for Streamlabs Bot"
Creator = "Bare7a"
Version = "1.3.2"

configFile = "config.json"
settings = {}
startTime = 0
bettingTime = 0
isBettingOpened = False
userList = []
pot = 0

def ScriptToggled(state):
	return

def Init():
	global settings

	path = os.path.dirname(__file__)
	try:
		with codecs.open(os.path.join(path, configFile), encoding='utf-8-sig', mode='r') as file:
			settings = json.load(file, encoding='utf-8-sig')
	except:
		settings = {
			"liveOnly": True,
			"command": "!raffle",
			"startCountdown" : 1800,
			"betCountdown" : 300,
			"permission": "Everyone",
			"minBet" : 1,
			"maxBet" : 100,
			"startedResponse" : "Raffle started! You can bet by typing $command <$min - $max>",
			"userBettedResponse" : "User betted $bet in raffle!",
			"userWonResponse" : "Raffle: $user won $win $currency!",
			"noBetsResponse" : "Raffle: nobody betted!",
			"notOpenedResponse" : "Raffle isn't opened right now!",
			"alreadyBettedResponse" : "$user, you are already betted in the raffle!",
			"wrongAmmountResponse" : "$user you can bet between $min - $max $currency. Currently you have $points $currency.",
		}
	return

def Execute(data):
	global userList, pot

	if data.IsChatMessage() and data.GetParam(0).lower() == settings["command"] and Parent.HasPermission(data.User, settings["permission"], "") and ((settings["liveOnly"] and Parent.IsLive()) or (not settings["liveOnly"])):
		outputMessage = ""
		userId = data.User
		username = data.UserName
		points = Parent.GetPoints(userId)
		bet = 0

		if (data.GetParamCount() == 2):
			try:
				bet = int(data.GetParam(1))
			except:
				if data.GetParam(1) == 'all':
					bet = points

		if not isBettingOpened:
			outputMessage = settings["notOpenedResponse"]
		elif (bet > points) or (bet < settings["minBet"]) or (bet > settings["maxBet"]):
			outputMessage = settings["wrongAmmountResponse"]
		elif any(username in user[1] for user in userList):
			outputMessage = settings["alreadyBettedResponse"]
		else:
			Parent.RemovePoints(userId, username, bet)
			pot += bet
			userList.append([userId, username, pot])
			outputMessage = settings["userBettedResponse"]

		outputMessage = outputMessage.replace("$user", username)
		outputMessage = outputMessage.replace("$bet", str(bet))
		outputMessage = outputMessage.replace("$min", str(settings["minBet"]))
		outputMessage = outputMessage.replace("$max", str(settings["maxBet"]))
		outputMessage = outputMessage.replace("$points", str(points))
		outputMessage = outputMessage.replace("$currency", Parent.GetCurrencyName())
		outputMessage = outputMessage.replace("$command", settings["command"])

		Parent.SendStreamMessage(outputMessage)
	return

def ReloadSettings(jsonData):
	Init()
	return

def OpenReadMe():
	location = os.path.join(os.path.dirname(__file__), "README.txt")
	os.startfile(location)
	return

def Tick():
	global startTime, bettingTime, isBettingOpened, userList, pot

	if not (settings["liveOnly"] and Parent.IsLive()) or (not settings["liveOnly"]):
		return

	currentTime = time.time()

	if(currentTime >= startTime):
		startTime = currentTime + settings["startCountdown"]
		bettingTime = currentTime + settings["betCountdown"]
		isBettingOpened = True

		outputMessage = settings["startedResponse"]
		outputMessage = outputMessage.replace("$min", str(settings["minBet"]))
		outputMessage = outputMessage.replace("$max", str(settings["maxBet"]))
		outputMessage = outputMessage.replace("$currency", Parent.GetCurrencyName())
		outputMessage = outputMessage.replace("$command", settings["command"])

		Parent.SendStreamMessage(outputMessage)

	if(currentTime >= bettingTime) and isBettingOpened:
		if pot > 0:
			winnerNumber = Parent.GetRandom(1, pot + 1)

		for	user in userList:
			if user[2] >= winnerNumber:
				Parent.AddPoints(user[0], user[1], pot)

				outputMessage = settings["userWonResponse"]
				outputMessage = outputMessage.replace("$user", user[1])
				outputMessage = outputMessage.replace("$currency", Parent.GetCurrencyName())
				outputMessage = outputMessage.replace("$win", str(pot))
				break
		else:
			outputMessage = settings["noBetsResponse"]

		isBettingOpened = False
		userList = []
		pot = 0

		Parent.SendStreamMessage(outputMessage)
	return


# -*- encoding: utf-8 -*-
"""
@Project    :   SourceHub
@File       :   urls.py
@Time       :   2021/4/13 0:49
@Author     :   Thooooor
@Version    :   1.0
@Contact    :   thooooor999@gmail.com
@Describe   :   None
"""
from django.urls import path

from . import views

app_name = "school"

urlpatterns = [
    path("school-list/", views.school_list, name="school-list"),
    path("school-detail/<int:id>/",views.school_detail, name="school-detail"),

]


import re

try:
    filename = input ("Enter The Filename: ")
    fhand = open (filename)

except:
    print ("Invalid File")

result = list()
Sum = 0

for line in fhand:
    line = line.rstrip()

    if re.search('[0-9]+', line):
        words = re.findall('[0-9]+', line)
        result = result + words

    else:
        continue

for i in range(len(result)):

    Sum = Sum + int(result[i])

print ("The Sum is", Sum)


import shutil
from pathlib import Path
from unittest import TestCase

import numpy as np

from platalea.dataset import _get_id_map, HowTo100MData

class Howto100mProcessingCase(TestCase):
    _dataset_path = Path('./testdata/howto100msample')  # Should only be accessed by setup.

    test_dataset_path = Path('./testdata/temp_howto100msample')
    id_map_file_name = 'long_id_map.json'
    audio_subdir = 'audio'
    video_subdir = 'video'
    video_features_subdir = 's3d_features'
    audio_features_file_name = 'dummy_mfcc_features.memmap'

    def _get_dataset(self, split):
        dataset = HowTo100MData(self.test_dataset_path,
                                self.audio_features_file_name,
                                self.video_features_subdir,
                                self.id_map_file_name,
                                split=split)
        return dataset

    def test_get_id_map_number_of_results(self):
        id_map_path = self.test_dataset_path / self.id_map_file_name
        assert len(_get_id_map(id_map_path, 'test')) == 1000, \
            'By default 1000 ids should go to the test set'

        assert len(_get_id_map(id_map_path, 'train')) == 3422, \
            'The other 3422 ids in the test file go to the train set'

    def test_load_item(self):
        dataset = self._get_dataset('train')
        item = dataset[0]
        print({k: item[k].shape for k in item if item[k] is not None and hasattr(item[k], 'shape')})
        print(item['id'])

    def test_evaluation_has_video(self):
        dataset = self._get_dataset('train')
        evaluation_data = dataset.evaluation()
        assert 'video' in evaluation_data

    def test_evaluation_has_audio(self):
        dataset = self._get_dataset('train')
        evaluation_data = dataset.evaluation()
        assert 'audio' in evaluation_data

    def test_evaluation_has_same_number_of_audio_video(self):
        dataset = self._get_dataset('test')
        evaluation_data = dataset.evaluation()
        assert len(evaluation_data['audio']) == len(evaluation_data['video'])

    def setUp(self):
        self._remove_temp_test_data_from_earlier_runs()

        shutil.copytree(self._dataset_path, self.test_dataset_path)
        # Create a test file
        np.memmap(self.test_dataset_path / self.audio_features_file_name, dtype='float64',
                  mode='w+', shape=(4422, 39))

    def _remove_temp_test_data_from_earlier_runs(self):
        """ Use of memmap makes cleanup during teardown unreliable.
            This function is just for convenience in case last teardown failed.
        """
        if self.test_dataset_path.exists():
            shutil.rmtree(self.test_dataset_path, ignore_errors=False)

    def tearDown(self):
        shutil.rmtree(self.test_dataset_path, ignore_errors=False)


'''Hydraulic functions for the Bay Assessment Model (BAM)'''

# Python distribution modules
from math import sqrt, pow

# Local modules
import constants

#---------------------------------------------------------------
#
#---------------------------------------------------------------
def Hydro( model ) :
    '''Calculation of water and mass fluxes over shoals is followed by
    mass balances for basins.

    At the beginning of each time step the velocity of water for each depth
    increment i of each shoal j is calculated using the water levels in
    adjacent basins and Manning's equation.

    The cross sectional area Aij of water for each depth of the shoal is
    calculated from the length of each wet_area at each depth increment
    of each shoal times a water level height.  If the downsteam water
    depth is positive, it is used as the height.  If not, the hydraulic
    radius of the flow is used.

    The flux of water across each shoal is calculated as Fj = Σ Vij * Aij.

    The fluxes Fj are multiplied by the time step duration and summed
    to give the net volume of water exchanged over the shoals.
    The net volume of water exchanged is added to the volume in the
    basin at the beginning of the time step. Other inputs of water during the
    time step are added to each basin (rain, ET, runoff, groundwater).
    The resulting new volume of water in each basin is used with the
    bathymetric data for the basin to calculate a new water level in the basin.

    The mass flux of dissolved substances Mj is calculated as the product of
    the concentration of the substance in the water Cj (g m-3) and the water
    flux on each shoal Mj = Fj * Cj.

    The mass fluxes Mj are summed around the boundary of each basin.
    The net mass flux over the shoals is multiplied by the time step and,
    along with other inputs and outputs of mass during the time step, added
    to the mass in the basin at the beginning of the time step. The new
    mass is divided by the water volume for the concentration at the end
    of the time step.
    '''
    pass

#---------------------------------------------------------------
#
#---------------------------------------------------------------
def ShoalVelocities( model ) :
    '''At the beginning of each step for each shoal the water level in the
    upstream basin is used to calculate the critical depth above the shoal.
    If the downstream water level is lower than h_crit there is sufficient
    head difference to create criticial flow. Velocity is estimated with an
    iterative solution between the two dependent variables of v and R
    (R = hydraulic radius = ratio of cross sectional flow area to the
    wetted perimeter, which for a shallow, broad cross section can be
    approximated by the average depth) via Cosby (2010) equations 1.13 & 1.15.

    If the downstream level is higher than h_crit, a non-critical solution
    is calculated using an iteration for the two dependent variables v and R
    via Cosby (2010) equations 1.10 & 1.11.

    In either case, the velocity from the last time step, v0, and the water
    levels from the current time step are used in equations 1.10 or 1.14 to
    estimate the initial hydraulic radius, R0. A first estimate of the velocity
    for the current time step, v1, is then calculated using the appropriate
    equation. The new velocity v1 is used to update the hydraulic radius R1,
    which is used to calculate a new velocity, v2. If v1 and v2 agree to
    within a specified tolerance, the v2 value is accepted.
    '''

    if model.args.DEBUG_ALL :
        print( '-> ShoalVelocities' )

    for shoal_number, Shoal in model.Shoals.items() :

        if model.args.DEBUG_ALL :
            if Shoal.Basin_A_key not in model.Basins.keys() or \
               Shoal.Basin_B_key not in model.Basins.keys() :
                continue

        # If shoal boundary is land (width zero) continue
        if Shoal.no_flow :
            continue

        # Process each shoal depth that has non-zero wet_length
        for depth_ft, length in Shoal.wet_length.items() :

            if length < 1 :
                continue

            # Initial depth estimate & itertation setup
            ShoalBasinLevels( model, Shoal, depth_ft )

            if Shoal.flow_sign == 0 :
                continue

            # Compute initial estimate of velocity and hydraulic radius
            # Use initial_velocity flag rather than checking :
            #       if depth_ft not in Shoal.velocity.keys() : ...
            if not Shoal.initial_velocity :
                VelocityHydraulicRadius( Shoal, depth_ft )

            previous_velocity = Shoal.velocity[ depth_ft ]

            # Update friction_factor for next iteration or timestep
            hydraulic_radius = Shoal.hydraulic_radius[ depth_ft ]
            if hydraulic_radius > 0 :
                Shoal.friction_factor[ depth_ft ] = \
                    2 * constants.g * \
                    pow( Shoal.manning_coefficient, 2 ) * \
                    Shoal.width * \
                    pow( hydraulic_radius, -4/3 )
            else :
                Shoal.friction_factor[ depth_ft ] = 1E9

            #--------------------------------------------------------
            # Iteration to estimate velocity and hydraulic.radius
            iteration_exceeded = True

            for i in range( 1, model.max_iteration ) :

                VelocityHydraulicRadius( Shoal, depth_ft )

                velocity = Shoal.velocity[ depth_ft ]

                delta_velocity = previous_velocity - velocity

                if abs( delta_velocity ) <= model.velocity_tol :
                    iteration_exceeded = False
                    break

                previous_velocity = velocity

            if iteration_exceeded :
                msg = '\n*** Mannings: iterations exceeded for shoal ' +\
                    str( shoal_number ) + ' at depth ' + str( depth_ft ) + '\n'
                model.gui.Message( msg )

        # Set flag that this shoal has been initialized
        Shoal.initial_velocity = True

#---------------------------------------------------------------
#
#---------------------------------------------------------------
def ShoalBasinLevels( model, Shoal, depth_ft ) :
    '''Determine shoal water level elevations h_upstream and h_downstream.
    h_critical, h_upstream and h_downstream are elevations with respect to
    the shoal top elevation (0 depth). All water level elevations are
    anomalies from the shoal elevation (0 depth), a convienent but limiting
    convention.

    Set flow_sign : -1 = Flow B -> A, 1 = Flow A -> B, 0 = No flow'''

    depth = depth_ft * 0.3048 # convert feet to meters

    Basin_A = Shoal.Basin_A
    Basin_B = Shoal.Basin_B

    # h_basin_* will be negative only if water_level is negative
    # and greater in magnitude than depth
    h_Basin_A = Basin_A.water_level + depth
    h_Basin_B = Basin_B.water_level + depth

    if h_Basin_A < 0 and h_Basin_B < 0 :
        # If water level is below shoal: no flow
        Shoal.h_upstream      [ depth_ft ] = h_Basin_A
        Shoal.h_downstream    [ depth_ft ] = h_Basin_B
        Shoal.friction_factor [ depth_ft ] = 1E9
        Shoal.velocity        [ depth_ft ] = 0
        Shoal.hydraulic_radius[ depth_ft ] = 0
        Shoal.flow_sign                    = 0     # No flow

    elif h_Basin_A > h_Basin_B :
        Shoal.h_upstream  [ depth_ft ] = h_Basin_A
        Shoal.h_downstream[ depth_ft ] = h_Basin_B
        Shoal.flow_sign                = 1         # Flow A -> B

    else :
        Shoal.h_upstream  [ depth_ft ] = h_Basin_B
        Shoal.h_downstream[ depth_ft ] = h_Basin_A
        Shoal.flow_sign                = -1         # Flow B -> A

#---------------------------------------------------------------
#
#---------------------------------------------------------------
def VelocityHydraulicRadius( Shoal, depth_ft ) :
    '''Compute flow velocity based on heads and flow regime.'''

    h_upstream      = Shoal.h_upstream     [ depth_ft ]
    h_downstream    = Shoal.h_downstream   [ depth_ft ]
    friction_factor = Shoal.friction_factor[ depth_ft ]

    h_critical = ( 2 * h_upstream ) / ( 3 + friction_factor )

    if h_downstream < h_critical :
        Shoal.h_downstream[ depth_ft ] = h_critical
        h_downstream                   = h_critical

    # If both h_ are negative : no flow
    level_difference       = h_upstream - h_downstream
    Shoal.level_difference = level_difference

    # Velocity head
    h_velocity = level_difference / ( 1 + friction_factor )

    # sqrt[ (m/s^2) * (m) ] = (m/s)
    Shoal.velocity[ depth_ft ] = Shoal.flow_sign * \
                                 sqrt( 2 * constants.g * h_velocity )

    # If the flow cross section is a wide, shallow rectangle, then the
    # average depth is an approximation of the hydraulic radius
    Shoal.hydraulic_radius[ depth_ft ] = \
        max( 0, ( h_upstream - h_velocity + h_downstream ) ) / 2

#---------------------------------------------------------------
#
#---------------------------------------------------------------
def MassTransport( model ) :
    '''See notes in Hydro'''

    if model.args.DEBUG_ALL :
        print( '-> MassTransport' )

    #--------------------------------------------------------------------
    # Sum the total flow over all shoal depths over the timestep
    #--------------------------------------------------------------------
    # Q(m^3/s) = v(m/s)   * A(m^2)
    # Vol(m^3) = Q(m^3/s) * dt(s)
    for shoal_number, Shoal in model.Shoals.items() :

        # If shoal boundary is land (zero width) continue
        if Shoal.no_flow :
            continue

        Basin_A = Shoal.Basin_A
        Basin_B = Shoal.Basin_B

        #--------------------------------------------------------
        # Process each shoal depth that has non-zero wet_length
        #--------------------------------------------------------
        for depth_ft, length in Shoal.wet_length.items() :

            if length < 1 :
                continue

            # This updates flow_sign, h_upstream, h_downstream at depth_ft
            ShoalBasinLevels( model, Shoal, depth_ft )

            if Shoal.flow_sign == 0 :
                Shoal.cross_section[ depth_ft ] = 0
                Shoal.Q            [ depth_ft ] = 0
                continue

            # What depth to use for the flow cross-sectional area:
            # upstream, downstream, or some median?
            # downstream makes sense since it's where the water is
            # flowing into the basin, but it can be negative
            h_flow = None

            h_downstream     = Shoal.h_downstream    [ depth_ft ]
            hydraulic_radius = Shoal.hydraulic_radius[ depth_ft ]

            if h_downstream > 0 :
                h_flow = h_downstream
            else :
                h_flow = hydraulic_radius

            cross_section = h_flow * Shoal.wet_length[ depth_ft ]

            if cross_section < 0 :
                raise ValueError ( 'Negative Shoal.cross_section' )

            Shoal.cross_section[ depth_ft ] = cross_section

            # Q(m^3/s) = v(m/s) * A(m^2)
            Shoal.Q[ depth_ft ] = Shoal.velocity[ depth_ft ] * cross_section

        # Sum flow across shoal (m^3/s)
        Shoal.Q_total             = sum( Shoal.Q.values() )
        Shoal.cross_section_total = sum( Shoal.cross_section.values() )

        # Transfer volumes across shoal into basins
        # The sign of Q handles the transfer direction
        # flow_sign positive : A -> B
        # flow_sign negative : B -> A
        delta_volume = Shoal.Q_total * model.timestep # (m^3/timestep)

        Shoal.volume_A_B =  delta_volume # (m^3/timestep)
        Shoal.volume_B_A = -delta_volume # (m^3/timestep)

        if not Basin_A.boundary_basin :
            Basin_A.water_volume -= delta_volume
        if not Basin_B.boundary_basin :
            Basin_B.water_volume += delta_volume

        # Shallow banks can have no volume at low stage
        # Limit the volume to a lower bound
        if Basin_A.water_volume < 0 :
            Basin_A.water_volume = 0
        if Basin_B.water_volume < 0 :
            Basin_B.water_volume = 0
        if Basin_A.water_volume == 0 or Basin_B.water_volume == 0 :
            continue  # don't transfer salt

        # Establish source basin and it's salinity
        if Shoal.flow_sign == 1 :     # flow from A -> B
            source_salinity = Basin_A.salinity
        elif Shoal.flow_sign == -1  : # flow from B -> A
            source_salinity = Basin_B.salinity
        else :
            source_salinity = 0

        # Transfer salt
        # delta salt_mass = salinity (g/kg) * Vol (m^3) * rho (kg/m^3)
        # Water at 25 C rho = 997 kg/m^3
        delta_salt_mass = source_salinity * delta_volume * 997

        if not Basin_A.boundary_basin :
            Basin_A.salt_mass -= delta_salt_mass

        if not Basin_B.boundary_basin :
            Basin_B.salt_mass += delta_salt_mass

        if Basin_A.salt_mass < 0 :
            Basin_A.salt_mass = 0
        if Basin_B.salt_mass < 0 :
            Basin_B.salt_mass = 0

    #----------------------------------------------------------------
    # Sum basin flow and compute salinity
    #----------------------------------------------------------------
    for Basin in model.Basins.values() :

        if Basin.boundary_basin :
            continue

        Basin.shoal_transport = 0

        for Shoal in Basin.Shoals :
            if Basin is Shoal.Basin_A :
                Basin.shoal_transport += Shoal.volume_A_B
            elif Basin is Shoal.Basin_B :
                Basin.shoal_transport += Shoal.volume_B_A
            else :
                raise Exception( 'Invalid Basin in Shoal' )

        # Salinity adjustment o/oo = g/kg = g / ( m^3 * (kg/m^3) )
        if not Basin.salinity_from_data and Basin.water_volume :
            new_salinity = Basin.salt_mass / ( Basin.water_volume * 997 )

            # On shallow banks a low stage/volume can spike the salinity
            # Log a message
            if new_salinity > 90 :
                if Basin.name not in [ 'First National Bank',
                                       'Ninemile Bank',  'Conchie Channel',
                                       'Johnson Key',    'Sandy Key',
                                       'Dildo Key Bank', 'Snake Bight',
                                       'Rankin Bight',   'Rankin Lake',
                                       'Deer Key',       'Swash Keys' ] :
                    msg = '*** MassTransport: Basin ' + Basin.name +\
                          ' old salinity: ' + str( round( Basin.salinity, 1)) +\
                          ' new salinity: ' + str( round( new_salinity, 1 ) ) +\
                          ' volume: '    + str( round( Basin.water_volume ) ) +\
                          ' salt mass: ' + str( round( Basin.salt_mass ) )    +\
                          ' at ' + str( model.current_time ) + '\n'
                    model.gui.Message( msg )
                    print( msg )

                # Ignore the new salinity...
                # Play God and wish some salt away...
                Basin.salt_mass = Basin.salt_mass * 0.5 # JP ???

            else :
                Basin.salinity = new_salinity

    #----------------------------------------------------------------
    # If EDEN stage is used to drive EVER runoff, sum the shoal transport
    # for reporting the total runoff in output data.
    #----------------------------------------------------------------
    if not model.args.noStageRunoff :

        for Basin, Shoals in model.runoff_stage_shoals.items() :

            Basin.runoff_EVER = 0

            for Shoal in Shoals :
                # All EVER runoff destination basins are Shoal.Basin_B
                # Flow sign convention is: flow out +, flow in -
                if Basin is Shoal.Basin_B :
                    Basin.runoff_EVER -= Shoal.volume_A_B
                else :
                    raise Exception( 'Invalid Basin in Shoal (Runoff)' )

#---------------------------------------------------------------
#
#---------------------------------------------------------------
def Depths( model ) :
    '''Update basin depths from volume changes.'''

    for Basin in model.Basins.values() :

        if not Basin.area : # Boundary basins have no area
            continue

        Basin.Area() # Update area at current water levels

        volume_difference = Basin.water_volume - Basin.previous_volume

        h_difference = volume_difference / Basin.area

        Basin.water_level += h_difference

        # Update previous_volume for next iteration
        Basin.previous_volume = Basin.water_volume


import matplotlib.pyplot as plt
import numpy as np
import hiprmc
from operator import mul
from skimage.draw import polygon, circle
from PIL import Image

def test_simulated():

    #####################################################
    ########## Begin Test Images ########################
    #####################################################

    N = 50  # size of image (NxN)
    initial_image = np.zeros((N, N))
    iterations = 2000

    # This bit creates a disc to try and model via RMC
    # for i in range(0, N):
    #     for j in range(0, N):
    #         r = np.sqrt((i - int(N / 2)) ** 2 + (j - int(N / 2)) ** 2)
    #         if 10 <= r <= 11:
    #             initial_image[i, j] = 1
    #         else:
    #             initial_image[i, j] = 0

    # # This creates a circle to model via RMC
    rr, cc = circle(int(N/2), int(N/2), int(N/8))
    initial_image[rr, cc] = 1

    # This creates a square
    #initial_image[int(2*N/6):int(4*N/6), int(1*N/6):int(2*N/6)] = 1

    #f_image = initial_image

    num_particles = np.count_nonzero(initial_image == 1)
    load = num_particles/initial_image.size

    i_image = hiprmc.abs2(hiprmc.fourier_transform(initial_image))

    T = 1000.0  # i_image.shape[0]  # temperature is on the same order as the image

    simulated_image = hiprmc.rmc(i_image, T, iterations, load, movie=True)

    f = plt.figure(3)
    f.add_subplot(2, 2, 1)
    plt.imshow(initial_image, cmap='Greys', origin="lower")
    plt.axis('off')
    plt.title("original image")
    f.add_subplot(1, 5, 2)
    plt.axis('off')
    f.add_subplot(2, 2, 2)
    plt.imshow(np.log10(i_image), origin="lower", cmap='hsv')
    # plt.imshow(cmap='Greys', origin='lower', alpha=0.5)
    plt.axis('off')
    plt.title("Initial Intensity Plot")
    f.add_subplot(2, 2, 3)
    plt.imshow(simulated_image, cmap="Greys", origin="lower")
    plt.title("Model")
    plt.axis('off')
    f.add_subplot(2, 2, 4)
    plt.imshow(np.log10(hiprmc.abs2(hiprmc.fourier_transform(simulated_image))), origin='lower', cmap='hsv')
    plt.axis('off')
    plt.title("Model Intensity Plot")
    plt.show()

    ######################################################
    ########### End Test Images ##########################
    ######################################################

def test_real():

    ######################################################
    ######### Uncomment for Uploading Real Data ##########
    ######################################################

    #Image upload
    image = np.load('cool_40p0C_ILC1_insitu_40pt0C_2m.edf.npy')
    print(image.shape)
    f_mask = np.load('shuai_data_mask.edf.npy')
    N = image.shape[0]
    image[image==np.nan]=0

    #set load of particles
    load = 0.10

    #random particles
    initial_particles = np.random.choice([0, 1], size=(N,N), p=[1.0-load, load])

    ######################################################
    ########### End Real Data Upload #####################
    ######################################################

    T = 40 # i_image.shape[0]  # temperature is on the same order as the image

    simulated_image = hiprmc.rmc(image, num_particles, N, T, iterations, movie=True)

    f = plt.figure(3)
    f.add_subplot(2, 2, 1)
    plt.imshow(image, cmap='Greys', origin="lower")
    plt.axis('off')
    plt.title("original image")
    f.add_subplot(1, 5, 2)
    plt.axis('off')
    #f.add_subplot(2, 2, 2)
    #original_intensity = np.log10(hiprmc.abs2(hiprmc.fourier_transform(i_image)))
    #plt.imshow(original_intensity, origin="lower", cmap='hsv')
    # plt.imshow(cmap='Greys', origin='lower', alpha=0.5)
    #plt.axis('off')
    #plt.title("Initial Intensity Plot")
    f.add_subplot(2, 2, 3)
    plt.imshow(simulated_image, cmap="Greys", origin="lower")
    plt.title("Model")
    plt.axis('off')
    f.add_subplot(2, 2, 4)
    plt.imshow(np.log10(hiprmc.abs2(hiprmc.fourier_transform(simulated_image))), origin='lower', cmap='hsv')
    plt.axis('off')
    plt.title("Model Intensity Plot")
    plt.show()

if __name__ == '__main__':
    test_simulated()


import pandas as pd
import lightgbm as lgb
from sklearn.model_selection import train_test_split, GridSearchCV
from sklearn.metrics import mean_squared_error
from sklearn.preprocessing import LabelEncoder

# 데이터를 데이터프레임으로 읽어오기
data = pd.read_excel('전력사용데이터_2013_2023.xlsx')  # 'your_data.xlsx'에 데이터 파일 경로를 입력하세요.

# 필요한 특성(features)과 타겟(target) 선택
features = ['year', 'month', 'metro', 'city']
target = 'powerUsage'  # 전력 사용량을 예측하려면 'powerUsage'를 사용합니다. 전기 요금을 예측하려면 'bill'을 사용합니다.

X = data[features]
y = data[target]

# 범주형 데이터를 인코딩
label_encoders = {}
for feature in features:
    if data[feature].dtype == 'object':
        label_encoders[feature] = LabelEncoder()
        X[feature] = label_encoders[feature].fit_transform(X[feature])

# 데이터를 학습 데이터와 테스트 데이터로 나누기
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

# LightGBM 데이터셋 생성
train_data = lgb.Dataset(X_train, label=y_train)
test_data = lgb.Dataset(X_test, label=y_test, reference=train_data)

# 그리드 탐색을 위한 하이퍼파라미터 그리드 설정
param_grid = {
    'boosting_type': ['gbdt', 'dart'],
    'num_leaves': [31, 50, 100],
    'learning_rate': [0.01, 0.05, 0.1],
    'feature_fraction': [0.9, 1.0],
    'num_round': [100, 200, 500]
}

# LightGBM 모델
model = lgb.LGBMRegressor()

# 그리드 탐색
grid_search = GridSearchCV(model, param_grid, cv=3, scoring='neg_mean_squared_error')
grid_search.fit(X_train, y_train)

# 최적 하이퍼파라미터 출력
print("Best Hyperparameters:")
print(grid_search.best_params_)

# 최적 모델 획득
best_model = grid_search.best_estimator_

# 모델 평가
y_pred = best_model.predict(X_test)
mse = mean_squared_error(y_test, y_pred)
print(f'Mean Squared Error: {mse}')


#-*- coding:utf-8-*-
'''
本代码是基于霍夫变换来做车道线检测的，个人感觉只针对直行道路有效。对于弯道，效果不好，其原因在于没有考虑到坐标转换下的效果。但canny概念倒是值得参考，具体可参见canny.py。
在笛卡尔坐标系中，我们可以通过绘制y对x来表示y = mx + b的直线。但是，我们也可以通过绘制b对m来将此线表示为霍夫空间中的单个点。
通常，在霍夫空间中相交的曲线越多意味着由该交点表示的线对应于更多的点。对于我们的实现，我们将在霍夫空间中定义最小阈值交叉点数以检测线。
因此，霍夫变换基本上跟踪帧中每个点的霍夫空间交叉点。如果交叉点的数量超过定义的阈值，我们将识别具有相应θ和r参数的线。
'''
import cv2
import os
import numpy as np
import math
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
from moviepy.editor import VideoFileClip

def do_canny(frame, kernel_size=5, low_threshold=50, high_threshold=150):
    # Converts frame to grayscale because we only need the luminance channel for detecting edges - less computationally expensive
    gray = cv2.cvtColor(frame, cv2.COLOR_RGB2GRAY)
    # Applies a 5x5 gaussian blur with deviation of 0 to frame - not mandatory since Canny will do this for us
    blur = cv2.GaussianBlur(gray, (kernel_size, kernel_size), 0)
    # Applies Canny edge detector with minVal of 50 and maxVal of 150
    canny = cv2.Canny(blur, low_threshold, high_threshold)
    return canny

def do_segment(frame, trap_bottom_width, trap_top_width, trap_height):
    # Since an image is a multi-directional array containing the relative intensities of each pixel in the image, we can use frame.shape to return a tuple: [number of rows, number of columns, number of channels] of the dimensions of the frame
    # frame.shape[0] give us the number of rows of pixels the frame has. Since height begins from 0 at the top, the y-coordinate of the bottom of the frame is its height
#    height = frame.shape[0]
    # Creates a triangular polygon for the mask defined by three (x, y) coordinates
#    polygons = np.array([[(0, height), (800, height), (380, 290)]])
    polygons = np.array([[\
        ((frame.shape[1] * (1 - trap_bottom_width)) // 2, frame.shape[0]), \
        ((frame.shape[1] * (1 - trap_top_width)) // 2, frame.shape[0] - frame.shape[0] * trap_height), \
        (frame.shape[1] - (frame.shape[1] * (1 - trap_top_width)) // 2, frame.shape[0] - frame.shape[0] * trap_height), \
        (frame.shape[1] - (frame.shape[1] * (1 - trap_bottom_width)) // 2, frame.shape[0])]], dtype=np.int32)
    # Creates an image filled with zero intensities with the same dimensions as the frame
    mask = np.zeros_like(frame)

    #defining a 3 channel or 1 channel color to fill the mask with depending on the input image
    if len(frame.shape) > 2:
        channel_count = frame.shape[2]
        ignore_mask_color = (255, ) * channel_count
    else:
        ignore_mask_color = 255

    # Allows the mask to be filled with values of 1 and the other areas to be filled with values of 0
    cv2.fillPoly(mask, polygons, ignore_mask_color)
    # A bitwise and operation between the mask and frame keeps only the triangular area of the frame
    segment = cv2.bitwise_and(frame, mask)
    return segment

def weighted_img(img, initial_img, α=0.8, β=1., λ=0.):
	"""
	`img` is the output of the hough_lines(), An image with lines drawn on it.
	Should be a blank image (all black) with lines drawn on it.

	`initial_img` should be the image before any processing.

	The result image is computed as follows:

	initial_img * α + img * β + λ
	NOTE: initial_img and img must be the same shape!
	"""
	return cv2.addWeighted(initial_img, α, img, β, λ)

def filter_colors(image):
	"""
	Filter the image to include only yellow and white pixels
	"""
	# Filter white pixels
	white_threshold = 200 #130
	lower_white = np.array([white_threshold, white_threshold, white_threshold])
	upper_white = np.array([255, 255, 255])
	white_mask = cv2.inRange(image, lower_white, upper_white)
	white_image = cv2.bitwise_and(image, image, mask=white_mask)

	# Filter yellow pixels
	hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
	lower_yellow = np.array([90,100,100])
	upper_yellow = np.array([110,255,255])
	yellow_mask = cv2.inRange(hsv, lower_yellow, upper_yellow)
	yellow_image = cv2.bitwise_and(image, image, mask=yellow_mask)

	# Combine the two above images
	image2 = cv2.addWeighted(white_image, 1., yellow_image, 1., 0.)

	return image2

def calculate_lines(frame, lines):
    # Empty arrays to store the coordinates of the left and right lines
    left = []
    right = []
    # Loops through every detected line
    for line in lines:
        # Reshapes line from 2D array to 1D array
        x1, y1, x2, y2 = line.reshape(4)
        # Fits a linear polynomial to the x and y coordinates and returns a vector of coefficients which describe the slope and y-intercept
        parameters = np.polyfit((x1, x2), (y1, y2), 1)
        slope = parameters[0]
        y_intercept = parameters[1]
        # If slope is negative, the line is to the left of the lane, and otherwise, the line is to the right of the lane
        if slope < 0:
            left.append((slope, y_intercept))
        else:
            right.append((slope, y_intercept))
    # Averages out all the values for left and right into a single slope and y-intercept value for each line
    left_avg = np.average(left, axis = 0)
    right_avg = np.average(right, axis = 0)
    # Calculates the x1, y1, x2, y2 coordinates for the left and right lines
    left_line = calculate_coordinates(frame, left_avg)
    right_line = calculate_coordinates(frame, right_avg)
    return np.array([left_line, right_line])

def calculate_coordinates(frame, parameters):
    slope, intercept = parameters
    # Sets initial y-coordinate as height from top down (bottom of the frame)
    y1 = frame.shape[0]
    # Sets final y-coordinate as 150 above the bottom of the frame
    y2 = int(y1 - 150)
    # Sets initial x-coordinate as (y1 - b) / m since y1 = mx1 + b
    x1 = int((y1 - intercept) / slope)
    # Sets final x-coordinate as (y2 - b) / m since y2 = mx2 + b
    x2 = int((y2 - intercept) / slope)
    return np.array([x1, y1, x2, y2])

def visualize_lines(frame, lines):
    # Creates an image filled with zero intensities with the same dimensions as the frame
    lines_visualize = np.zeros_like(frame)
    # Checks if any lines are detected
    if lines is not None:
        for x1, y1, x2, y2 in lines:
            # Draws lines between two coordinates with green color and 5 thickness
            cv2.line(lines_visualize, (x1, y1), (x2, y2), (0, 255, 0), 5)
    return lines_visualize

#trap_height height of the trapezoid expressed as percentage of image height
def draw_lines(img, lines, color=[255, 0, 0], thickness=10, trap_height=0.4):
	"""
	NOTE: this is the function you might want to use as a starting point once you want to
	average/extrapolate the line segments you detect to map out the full
	extent of the lane (going from the result shown in raw-lines-example.mp4
	to that shown in P1_example.mp4).

	Think about things like separating line segments by their
	slope ((y2-y1)/(x2-x1)) to decide which segments are part of the left
	line vs. the right line.  Then, you can average the position of each of
	the lines and extrapolate to the top and bottom of the lane.

	This function draws `lines` with `color` and `thickness`.
	Lines are drawn on the image inplace (mutates the image).
	If you want to make the lines semi-transparent, think about combining
	this function with the weighted_img() function below
	"""
	# In case of error, don't draw the line(s)
	if lines is None:
		return
	if len(lines) == 0:
		return
	draw_right = True
	draw_left = True

	# Find slopes of all lines
	# But only care about lines where abs(slope) > slope_threshold
	slope_threshold = 0.5
	slopes = []
	new_lines = []
	for line in lines:
		x1, y1, x2, y2 = line[0]  # line = [[x1, y1, x2, y2]]

		# Calculate slope
		if x2 - x1 == 0.:  # corner case, avoiding division by 0
			slope = 999.  # practically infinite slope
		else:
			slope = (y2 - y1) / (x2 - x1)

		# Filter lines based on slope
		if abs(slope) > slope_threshold:
			slopes.append(slope)
			new_lines.append(line)

	lines = new_lines

	# Split lines into right_lines and left_lines, representing the right and left lane lines
	# Right/left lane lines must have positive/negative slope, and be on the right/left half of the image
	right_lines = []
	left_lines = []
	for i, line in enumerate(lines):
		x1, y1, x2, y2 = line[0]
		img_x_center = img.shape[1] / 2  # x coordinate of center of image
		if slopes[i] > 0 and x1 > img_x_center and x2 > img_x_center:
			right_lines.append(line)
		elif slopes[i] < 0 and x1 < img_x_center and x2 < img_x_center:
			left_lines.append(line)

	# Run linear regression to find best fit line for right and left lane lines
	# Right lane lines
	right_lines_x = []
	right_lines_y = []

	for line in right_lines:
		x1, y1, x2, y2 = line[0]

		right_lines_x.append(x1)
		right_lines_x.append(x2)

		right_lines_y.append(y1)
		right_lines_y.append(y2)

	if len(right_lines_x) > 0:
		right_m, right_b = np.polyfit(right_lines_x, right_lines_y, 1)  # y = m*x + b
	else:
		right_m, right_b = 1, 1
		draw_right = False

	# Left lane lines
	left_lines_x = []
	left_lines_y = []

	for line in left_lines:
		x1, y1, x2, y2 = line[0]

		left_lines_x.append(x1)
		left_lines_x.append(x2)

		left_lines_y.append(y1)
		left_lines_y.append(y2)

	if len(left_lines_x) > 0:
		left_m, left_b = np.polyfit(left_lines_x, left_lines_y, 1)  # y = m*x + b
	else:
		left_m, left_b = 1, 1
		draw_left = False

	# Find 2 end points for right and left lines, used for drawing the line
	# y = m*x + b --> x = (y - b)/m
	y1 = img.shape[0]
	y2 = img.shape[0] * (1 - trap_height)

	right_x1 = (y1 - right_b) / right_m
	right_x2 = (y2 - right_b) / right_m

	left_x1 = (y1 - left_b) / left_m
	left_x2 = (y2 - left_b) / left_m

	# Convert calculated end points from float to int
	y1 = int(y1)
	y2 = int(y2)
	right_x1 = int(right_x1)
	right_x2 = int(right_x2)
	left_x1 = int(left_x1)
	left_x2 = int(left_x2)

	# Draw the right and left lines on image
	if draw_right:
		cv2.line(img, (right_x1, y1), (right_x2, y2), color, thickness)
	if draw_left:
		cv2.line(img, (left_x1, y1), (left_x2, y2), color, thickness)

def annotate_image_array(image_in):
    frame = filter_colors(image_in)

    canny = do_canny(frame, kernel_size=5, low_threshold=50, high_threshold=150)
    cv2.imshow("canny", canny)

    # Region-of-interest vertices
    # We want a trapezoid shape, with bottom edge at the bottom of the image
    # trap_bottom_width = 0.85  # width of bottom edge of trapezoid, expressed as percentage of image width
    # trap_top_width = 0.07  # ditto for top edge of trapezoid
    # trap_height = 0.4  # height of the trapezoid expressed as percentage of image height
    segment = do_segment(canny, 0.85, 0.07, 0.4)

    # Hough Transform
    #rho = 2 # distance resolution in pixels of the Hough grid
    #theta = 1 * np.pi/180 # angular resolution in radians of the Hough grid
    #threshold = 15	 # minimum number of votes (intersections in Hough grid cell)
    #min_line_length = 10 #minimum number of pixels making up a line
    #max_line_gap = 20	# maximum gap in pixels between connectable line segments
    #lines = cv2.HoughLinesP(img, rho, theta, threshold, np.array([]), minLineLength=10, maxLineGap=20)
    #lines = cv2.HoughLinesP(segment, 2, np.pi / 180, 100, np.array([]), minLineLength = 100, maxLineGap = 50)
    lines = cv2.HoughLinesP(segment, 2, 1 * np.pi/180, 15, np.array([]), minLineLength=10, maxLineGap=20)
    if 1:
        line_img = np.zeros((*segment.shape, 3), dtype=np.uint8)  # 3-channel RGB image
        draw_lines(line_img, lines)

        # Draw lane lines on the original image
        initial_image = image_in.astype('uint8')
        annotated_image = weighted_img(line_img, initial_image)
    else:

        # Averages multiple detected lines from hough into one line for left border of lane and one line for right border of lane
        lines_img = calculate_lines(image_in, lines)
        # Visualizes the lines
        lines_visualize = visualize_lines(image_in, lines_img)
        cv2.imshow("hough", lines_visualize)

        # Overlays lines on frame by taking their weighted sums and adding an arbitrary scalar value of 1 as the gamma argument
        output = cv2.addWeighted(image_in, 0.9, lines_visualize, 1, 1)

    return annotated_image

if __name__ == "__main__":
    cap = cv2.VideoCapture("F:\\developing\\this_week\\lane-detection\\advanced_lane_detection-master\\challenge_video.mp4")
    while (cap.isOpened()):
        ret, frame = cap.read()
        output = annotate_image_array(frame)
        # Opens a new window and displays the output frame
        cv2.imshow("output", output)

        if cv2.waitKey(10) & 0xFF == ord('q'):
            break

    cap.release()
    cv2.destroyAllWindows()


# -*- coding: utf-8 -*-

from odoo import fields, models
from datetime import datetime
from .. import surya

# Notes
class Notes(models.Model):
    _name = "hos.notes"
    _rec_name = "person_id"
    _inherit = "mail.thread"

    date = fields.Datetime(string="Date",
                           default=datetime.now().strftime("%Y-%m-%d"),
                           required=True)
    person_id = fields.Many2one(comodel_name="hos.person",
                                default=lambda self: self.env.user.person_id.id,
                                string="Person",
                                readonly=True)
    message = fields.Text(string="Message", required=True)
    company_id = fields.Many2one(comodel_name="res.company",
                                 string="Company",
                                 default=lambda self: self.env.user.company_id.id,
                                 readonly=True)


from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from webdriver_manager.chrome import ChromeDriverManager

import time
import re
import xlwings as xw
import pandas as pd

url = 'https://www.pvoil.com.vn/truyen-thong/tin-gia-xang-dau'
service = Service(executable_path=ChromeDriverManager().install())
options = Options()

driver = webdriver.Chrome(service=service, options=options)
driver.get(url)
html_content = driver.page_source
with open('html_content.txt', 'w', encoding='utf-8') as f:
    f.write(html_content)
    f.close()
with open('html_content.txt', 'r', encoding='utf-8') as f:
    data_1 = f.read()
    f.close()

pattern = '<option value="(.+)">.+</option>'
time_ = re.findall(pattern,data_1)
print(time_)
df_all = pd.DataFrame()
for t in time_:
    driver.find_element(By.XPATH,f"//select/option[@value='{t}']").click();
    time.sleep(2)
    html_content_item = driver.page_source
    with open('html_content_item.txt', 'w', encoding='utf-8') as f:
        f.write(html_content_item)
        f.close()
    with open('html_content_item.txt', 'r', encoding='utf-8') as f:
        data = f.read()
        f.close()
#
    pattern_time_update = '<option selected="selected" value="(.+)">.+</option>'
    time_update = re.findall(pattern_time_update,data)

    pattern = '(.+)	'
    item = re.findall(pattern,data)

    pattern = '(.+)	'
    price_1 = re.findall(pattern,data)

    price_update = []
    gap = []
    for x in range(len(price_1)):
        if x%2==0:
            price_update.append(price_1[x])
        else:
            gap.append(price_1[x])
    price_update = [x.replace(".","") for x in price_update]

    date_update = []
    for i in range(len(price_update)):
        date_update.append(time_update[0])
    data = list(zip(date_update,item,price_update,gap))
    columns = ['date_update','item', 'price_update', 'gap']
    df = pd.DataFrame(data=data,columns=columns)
    df_all = pd.concat([df,df_all])
df_all.to_excel('Petrol_Price.xlsx')


import pytest
from math import isclose

from pygrf import open_act

@pytest.mark.parametrize('name, version', (
    ('cursors.act', 0x203),
    ('agav.act', 0x205),
))
def test_act_has_correct_version(data_files, name, version):
    act = open_act(data_files[name])
    assert act.version == version

@pytest.mark.parametrize('name, count', (
    ('cursors.act', 13),
    ('agav.act', 40),
))
def test_act_has_correct_animation_count(data_files, name, count):
    act = open_act(data_files[name])
    assert len(act.animations) == count

@pytest.mark.parametrize('name, count', (
    ('cursors.act', 0),
    ('agav.act', 5),
))
def test_act_has_correct_trigger_count(data_files, name, count):
    act = open_act(data_files[name])
    assert len(act.triggers) == count

def test_act_has_correct_triggers(data_files):
    act = open_act(data_files['agav.act'])
    triggers = (
        "vanberk_move.wav",
        "vanberk_attack.wav",
        "vanberk_damage.wav",
        "vanberk_die.wav",
        "atk",
    )
    for idx, trigger in enumerate(triggers):
        assert act.triggers[idx] == trigger

@pytest.mark.parametrize('name, interval', (
    ('cursors.act', 2.0),
    ('agav.act', 4.0),
))
def test_act_animation_has_correct_interval(data_files, name, interval):
    act = open_act(data_files[name])
    assert act.animations[0].interval == interval

@pytest.mark.parametrize('name, counts', (
    ('cursors.act', (11, 9, 3)),
    ('agav.act', (4, 4, 4)),
))
def test_act_animation_has_correct_frame_count(data_files, name, counts):
    act = open_act(data_files[name])
    assert len(act.animations[0].frames) == counts[0]
    assert len(act.animations[1].frames) == counts[1]
    assert len(act.animations[2].frames) == counts[2]

def test_act_frame_has_correct_layer_count(data_files):
    act = open_act(data_files['cursors.act'])
    assert len(act.animations[0].frames[0].layers) == 1
    assert len(act.animations[1].frames[8].layers) == 4

def test_act_frame_has_correct_trigger_id(data_files):
    act = open_act(data_files['agav.act'])
    assert act.animations[0].frames[0].trigger == -1
    assert act.animations[8].frames[7].trigger == 0

def test_act_layer_has_correct_position(data_files):
    act = open_act(data_files['cursors.act'])
    assert act.animations[0].frames[0].layers[0].offset == (11, 15)
    assert act.animations[1].frames[1].layers[0].offset == (0, -1)

def test_act_layer_has_correct_index(data_files):
    act = open_act(data_files['cursors.act'])
    assert act.animations[0].frames[0].layers[0].index == 0
    assert act.animations[1].frames[1].layers[0].index == 16

@pytest.mark.parametrize('name, flipped', (
    ('cursors.act', False),
    ('agav.act', True),
))
def test_act_layer_is_correctly_flipped(data_files, name, flipped):
    act = open_act(data_files[name])
    assert act.animations[0].frames[0].layers[0].flipped == flipped

@pytest.mark.parametrize('name, color', (
    ('cursors.act', (255, 255, 255, 245)),
    ('agav.act', (255, 255, 255, 255)),
))
def test_act_layer_has_correct_color(data_files, name, color):
    act = open_act(data_files[name])
    assert act.animations[0].frames[0].layers[0].color == color

def test_act_layer_has_correct_zoom(data_files):
    act = open_act(data_files['cursors.act'])
    assert act.animations[0].frames[0].layers[0].zoom == (1.0, 1.0)
    zoom = act.animations[1].frames[1].layers[0].zoom
    assert isclose(zoom.x, 1.2, abs_tol=0.0001)
    assert isclose(zoom.y, 1.2, abs_tol=0.0001)

def test_act_layer_has_correct_angle(data_files):
    act = open_act(data_files['agav.act'])
    assert act.animations[0].frames[0].layers[0].angle == 0.0
    angle = act.animations[24].frames[3].layers[0].angle
    assert isclose(angle, 2.8026e-45, rel_tol=0.00001)


"""
Given a signed 32-bit integer x, return x with its digits reversed. If reversing x causes the value to go outside
the signed 32-bit integer range [-2**31, 2**31 - 1], then return 0.

Assume the environment does not allow you to store 64-bit integers (signed or unsigned).

Example 1:

Input: x = 123
Output: 321
Example 2:

Input: x = -123
Output: -321
Example 3:

Input: x = 120
Output: 21

Constraints:

-2**31 <= x <= 2**31 - 1
"""

class Solution:
    def reverse(self, x: int) -> int:
        if x < 0:
            positive = False
        else:
            positive = True
        x = abs(x)
        res = 0
        while x > 0:
            res = res * 10 + x % 10
            x = x // 10

        if positive:
            res = res
        else:
            res = -res
        if res > 2**31 - 1 or res < -2**31:
            res = 0
        return res


import os.path as osp

from mmocr.datasets.builder import LOADERS, build_parser
from mmocr.utils import list_from_file, list_from_folder_table

import pickle

@LOADERS.register_module()
class Loader:
    """Load annotation from annotation file, and parse instance information to
    dict format with parser.

    Args:
        ann_file (str): Annotation file path.
        parser (dict): Dictionary to construct parser
            to parse original annotation infos.
        repeat (int): Repeated times of annotations.
    """

    def __init__(self, ann_file, parser, repeat=1, max_seq_len=40):
        assert isinstance(ann_file, str)
        assert isinstance(repeat, int)
        assert isinstance(parser, dict)
        assert repeat > 0
        assert osp.exists(ann_file), f'{ann_file} is not exist'

        self.max_seq_len = max_seq_len
        self.ori_data_infos = self._load(ann_file)
        self.parser = build_parser(parser)
        self.repeat = repeat

    def __len__(self):
        return len(self.ori_data_infos) * self.repeat

    def _load(self, ann_file):
        """Load annotation file."""
        raise NotImplementedError

    def __getitem__(self, index):
        """Retrieve anno info of one instance with dict format."""
        return self.parser.get_item(self.ori_data_infos, index)

    def __iter__(self):
        self._n = 0
        return self

    def __next__(self):
        if self._n < len(self):
            data = self[self._n]
            self._n += 1
            return data
        raise StopIteration

@LOADERS.register_module()
class HardDiskLoader(Loader):
    """Load annotation file from hard disk to RAM.

    Args:
        ann_file (str): Annotation file path.
    """

    def _load(self, ann_file):
        return list_from_file(ann_file)

@LOADERS.register_module()
class TableHardDiskLoader(Loader):
    """Load table structure recognition annotation file from hard disk to RAM.

    Args:
        ann_files_folder (str): Annotation file folder.
    """

    def _load(self, ann_files_folder):
        return list_from_folder_table(ann_files_folder, self.max_seq_len)

@LOADERS.register_module()
class LmdbLoader(Loader):
    """Load annotation file with lmdb storage backend."""

    def _load(self, ann_file):
        lmdb_anno_obj = LmdbAnnFileBackend(ann_file)

        return lmdb_anno_obj

class LmdbAnnFileBackend:
    """Lmdb storage backend for annotation file.

    Args:
        lmdb_path (str): Lmdb file path.
    """

    def __init__(self, lmdb_path, coding='utf8'):
        self.lmdb_path = lmdb_path
        self.coding = coding
        env = self._get_env()
        with env.begin(write=False) as txn:
            self.total_number = int(
                txn.get('total_number'.encode(self.coding)).decode(
                    self.coding))

    def __getitem__(self, index):
        """Retrieval one line from lmdb file by index."""
        # only attach env to self when __getitem__ is called
        # because env object cannot be pickle
        if not hasattr(self, 'env'):
            self.env = self._get_env()
        with self.env.begin(write=False) as txn:
            line = txn.get(str(index).encode(self.coding)).decode(self.coding)
        return line

    def __len__(self):
        return self.total_number

    def _get_env(self):
        import lmdb
        return lmdb.open(
            self.lmdb_path,
            max_readers=1,
            readonly=True,
            lock=False,
            readahead=False,
            meminit=False,
        )

@LOADERS.register_module()
class MJSTLmdbLoader(Loader):
    """Load annotation file with lmdb storage backend with FastOCR lmdb style."""

    def _load(self, ann_file):
        lmdb_anno_obj = MJSTLmdbAnnFileBackend(ann_file)

        return lmdb_anno_obj

class MJSTLmdbAnnFileBackend:
    """Lmdb storage backend for annotation file FastOCR lmdb style.

    Args:
        lmdb_path (str): Lmdb file path.
    """

    def __init__(self, lmdb_path, coding='utf8'):
        self.lmdb_path = lmdb_path
        self.coding = coding
        env = self._get_env()
        with env.begin(write=False) as txn:
            self.total_number = int(txn.get(b"num-samples"))

    def __getitem__(self, index):
        """Retrieval one line from lmdb file by index."""
        # only attach env to self when __getitem__ is called
        # because env object cannot be pickle
        if not hasattr(self, 'env'):
            self.env = self._get_env()

        # MJST lmdb file is start as index 1, and offset 1 ...
        index = index + 1
        # return index as filename for lmdb image reading.
        label_key, filename = b'label-%09d' % index, r'%s' % index
        separator = ' '
        with self.env.begin(write=False) as txn:
            line = filename + separator + txn.get(label_key).decode()
        return line

    def __len__(self):
        return self.total_number

    def _get_env(self):
        import lmdb
        return lmdb.open(
            self.lmdb_path,
            max_readers=1,
            readonly=True,
            lock=False,
            readahead=False,
            meminit=False,
        )

@LOADERS.register_module()
class TableMASTERLmdbLoader(Loader):
    """Load TableMASTER annotation file with lmdb storage backend with FastOCR lmdb style."""

    def _load(self, ann_file):
        lmdb_anno_obj = TableMASTERLmdbAnnFileBackend(ann_file)

        return lmdb_anno_obj

class TableMASTERLmdbAnnFileBackend:
    """TableMASTER lmdb storage backend for annotation file FastOCR lmdb style.
        Create lmdb file by 'lmdb_maker.py'

    Args:
        lmdb_path (str): Lmdb file path.
    """

    def __init__(self, lmdb_path, coding='utf8'):
        self.lmdb_path = lmdb_path
        self.coding = coding
        env = self._get_env()
        with env.begin(write=False) as txn:
            self.total_number = int(pickle.loads(txn.get(b"__len__")))

    def __getitem__(self, index):
        """Retrieval one line from lmdb file by index."""
        # only attach env to self when __getitem__ is called
        # because env object cannot be pickle
        if not hasattr(self, 'env'):
            self.env = self._get_env()

        with self.env.begin(write=False) as txn:
            # data will be further parsed in parser.
            data = pickle.loads(txn.get(str.encode('{}'.format(index))))
        return data

    def __len__(self):
        return self.total_number

    def _get_env(self):
        import lmdb
        return lmdb.open(
            self.lmdb_path,
            max_readers=1,
            readonly=True,
            lock=False,
            readahead=False,
            meminit=False,
        )

@LOADERS.register_module()
class MASTERLmdbLoader(Loader):
    """Load text-line MASTER annotation file with lmdb storage backend with FastOCR lmdb style."""

    def _load(self, ann_file):
        lmdb_anno_obj = MASTERLmdbAnnFileBackend(ann_file)

        return lmdb_anno_obj

class MASTERLmdbAnnFileBackend:
    """Text-line MASTER lmdb storage backend for annotation file FastOCR lmdb style.
        Create lmdb file by 'lmdb_maker.py'

    Args:
        lmdb_path (str): Lmdb file path.
    """

    def __init__(self, lmdb_path, coding='utf8'):
        self.lmdb_path = lmdb_path
        self.coding = coding
        env = self._get_env()
        with env.begin(write=False) as txn:
            self.total_number = int(pickle.loads(txn.get(b"__len__")))

    def __getitem__(self, index):
        """Retrieval one line from lmdb file by index."""
        # only attach env to self when __getitem__ is called
        # because env object cannot be pickle
        if not hasattr(self, 'env'):
            self.env = self._get_env()

        with self.env.begin(write=False) as txn:
            # data will be further parsed in parser.
            data = pickle.loads(txn.get(str.encode('{}'.format(index))))
        return data

    def __len__(self):
        return self.total_number

    def _get_env(self):
        import lmdb
        return lmdb.open(
            self.lmdb_path,
            max_readers=1,
            readonly=True,
            lock=False,
            readahead=False,
            meminit=False,
        )


import asyncio
import unittest
import json

from lib.dispatcher import Dispatcher
from lib.channel import Channel
from lib.client import Client
from lib.message import Message

class MockAsyncIterator:
    def __init__(self, iterable):
        self.iterable = iterable

    def __aiter__(self):
        return self

    async def __anext__(self):
        try:
            return next(self.iterable)
        except StopIteration:
            raise StopAsyncIteration

class DispatcherTests(unittest.TestCase):
    def setUp(self):
        self.target_channel = Channel("target_channel")
        self.target_channel.broadcast = unittest.mock.AsyncMock()
        self.target_channel.send = unittest.mock.AsyncMock()
        self.another_channel = Channel("another_channel")
        self.channels = {
                self.target_channel.id: self.target_channel,
                self.another_channel.id: self.another_channel
                }
        self.sender_mock_socket = MockAsyncIterator(iter([]))
        self.sender = Client(self.sender_mock_socket)
        self.receiver_mock_socket = MockAsyncIterator(iter([]))
        self.receiver = Client(self.receiver_mock_socket)
        self.sample_message = Message(self.sender.address,self.receiver.address,self.target_channel.id,"")

    def test_init(self):
        expected_channels = {
            "channel1": Channel("channel1"),
            "channel2": Channel("channel2")
                }
        dispatcher = Dispatcher(expected_channels)
        self.assertEqual(dispatcher.channels, expected_channels)

    def test_create_channel(self):

        message = self.sample_message
        message.type = "create_channel"
        message.receiver_guid = ""
        message.data = {"name": "new_channel"}

        # Mock the webocket methods
        self.sender_mock_socket.send = unittest.mock.AsyncMock()

        dispatcher = Dispatcher({})
        self.mock_receive_message(message, dispatcher)

        # It adds a new channel
        found = False
        new_channel = ""
        for channel in dispatcher.channels.values():
            found = channel.name == "new_channel"
            if found:
                new_channel = channel
                break
        self.assertTrue(found)

        # It sends an 'ack' with the channel id
        self.sender_mock_socket.send.assert_called()
        sent_message = Message.from_json(self.sender_mock_socket.send.call_args_list[0][0][0])
        self.assertEqual(sent_message.type, "ack")
        self.assertEqual(sent_message.data["channel_id"], new_channel.id)

    def test_client_disconnect(self):
        message = self.sample_message
        message.type = "ignored"
        message.channel_id = self.target_channel.id

        dispatcher = Dispatcher(self.channels)
        self.mock_receive_message(message, dispatcher)

        # It broadcasts a hangup
        self.target_channel.broadcast.assert_called()
        sent_message = self.target_channel.broadcast.call_args_list[0][0][1]
        self.assertEqual(sent_message.type, "hangup")

        # It removes the client from the channel
        self.assertEqual(len(self.target_channel.clients), 0)

    def test_message_with_missing_channel(self):
        # When channel_id is missing

        message = self.sample_message
        message.type = "anything_but_create_channel"
        message.channel_id = "non_existent"
        message.receiver_guid = ""

        # Mock the webocket methods
        self.sender_mock_socket.close = unittest.mock.AsyncMock()
        self.sender_mock_socket.send = unittest.mock.AsyncMock()

        # Receive the test message
        dispatcher = Dispatcher(self.channels)
        self.mock_receive_message(message, dispatcher)

        # It sends an error to the sender
        self.sender_mock_socket.send.assert_called()
        sent_message = Message.from_json(self.sender_mock_socket.send.call_args_list[0][0][0])
        self.assertEqual(sent_message.type, "error")

        # It disconnects
        self.sender_mock_socket.close.assert_called()

    def test_announce_message(self):
        # When an "announce" is received

        message = self.sample_message
        message.type = "announce"
        message.receiver_guid = ""

        # It broadcasts it to the channel
        self.it_broadcasts(message)

        # It adds the sender to the channel
        matching_clients = list(client for client in list(self.target_channel.clients) if client.websocket == self.sender.websocket)
        self.assertEqual(len(matching_clients), 1)

    def test_name_message(self):
        message = self.sample_message
        message.type = "name"
        message.receiver_guid = ""

        # It broadcasts it to the channel
        self.it_broadcasts(message)

    def test_offer_message(self):
        message = self.sample_message
        message.type = "offer"
        self.it_sends(message)

    def test_answer_message(self):
        message = self.sample_message
        message.type = "answer"
        self.it_sends(message)

    def test_ice_message(self):
        message = self.sample_message
        message.type = "ice"
        self.it_sends(message)

    def it_broadcasts(self, message):
        self.target_channel.remove_client = unittest.mock.Mock()
        self.sender_mock_socket.iterable = iter([message.to_json()])

        dispatcher = Dispatcher(self.channels)

        asyncio.get_event_loop().run_until_complete(
                dispatcher.handle_connection(self.sender_mock_socket, "/")
        )

        # It broadcasts it to the channel
        self.target_channel.broadcast.assert_called()
        broadcast_args = self.target_channel.broadcast.call_args_list[0]
        self.assertEqual(self.sender.websocket, broadcast_args[0][0].websocket)
        self.assertEqual(vars(message), vars(broadcast_args[0][1]))

    def it_sends(self, message):
        self.target_channel.remove_client = unittest.mock.Mock()
        self.sender_mock_socket.iterable = iter([message.to_json()])

        self.target_channel.add_client(self.sender)
        dispatcher = Dispatcher(self.channels)

        asyncio.get_event_loop().run_until_complete(
                dispatcher.handle_connection(self.sender_mock_socket, "/")
        )

        # It sends the message
        self.target_channel.send.assert_called()
        send_args = self.target_channel.send.call_args_list[0]
        self.assertEqual(self.sender.websocket, send_args[0][0].websocket)
        self.assertEqual(vars(message), vars(send_args[0][1]))

    def mock_receive_message(self, message, dispatcher):
        self.target_channel.remove_client = unittest.mock.Mock()
        self.sender_mock_socket.iterable = iter([message.to_json()])

        asyncio.get_event_loop().run_until_complete(
                dispatcher.handle_connection(self.sender_mock_socket, "/")
        )


#!/usr/bin/env python
import os,sys,inspect
currentdir = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))
sys.path.insert(0,os.path.dirname(currentdir))

import numpy as np
import time

from Modelling.system_params import SystemParams
import Helpers.kinematics_helper as kh
from Helpers.ros_manager import ros_manager

import rospy

if __name__ == '__main__':
    apriltag_id = 10

    sys_params = SystemParams()

    # initialize node
    node_name = 'ee_apriltag_pose_in_world_from_camera'
    rm = ros_manager()
    rospy.init_node(node_name)
    rate = rospy.Rate(2*sys_params.hal_params['CAMERA_RATE'])

    # Make listener and get vicon to workobj rotation
    rm.spawn_transform_listener()

    rm.subscribe('/tag_detections')
    rm.spawn_publisher('/ee_apriltag_in_world')

    rm.wait_for_necessary_data()

    # camera frame in base frame
    # (cam_in_base_trans, cam_in_base_rot) = rm.lookupTransform('/far_camera_color_optical_frame', 'base')
    (cam_in_base_trans, cam_in_base_rot) = rm.lookupTransform('/near_camera_color_optical_frame', 'base')

    cam_in_base_pose_list = cam_in_base_trans + cam_in_base_rot

    # base frame in base frame
    base_in_base_pose_list = kh.unit_pose_list()

    rm.init_time_logger(node_name)

    # Run node at rate
    while not rospy.is_shutdown():
        rm.tl_reset()
        rm.unpack_all()

        if apriltag_id in rm.apriltag_pose_list_dict:
            # ee apriltag pose in camera frame
            ee_apriltag_in_camera_pose_list = rm.apriltag_pose_list_dict[apriltag_id]

            # convert ee apriltag pose from camera to base
            ee_apriltag_in_world_pose_list = kh.convert_reference_frame(ee_apriltag_in_camera_pose_list, base_in_base_pose_list,cam_in_base_pose_list)

            # publish
            rm.pub_ee_apriltag_frame(ee_apriltag_in_world_pose_list)

        rm.log_time()
        rate.sleep()


from typing import List

from aiogram import types, Dispatcher
from aiogram.dispatcher import FSMContext
from aiogram.dispatcher.filters.state import State, StatesGroup
import asyncio

from src.core.config import bot
from src.core.enums import BotMessageText, CallbackData, CacheKeys
from src.core.secrets import CHAT_ID, MASTER_ADMIN
from src.db.query import create_feedback
from src.keyboards import main_menu_client, back_to_menu, main_menu_admin
from src.utils.cache import get_cache

# define finite-state machine
class FSMFeedback(StatesGroup):
    feedback = State()

async def leave_feedback(callback_query: types.CallbackQuery, state: FSMContext):
    # exit FSM (if not finished)
    await state.finish()
    # ask to enter feedback
    await callback_query.message.edit_text(
        text=BotMessageText.ask_feedback.value,
        parse_mode='HTML',
        reply_markup=back_to_menu(section=CallbackData.main_menu.value)
    )
    # set the first state
    await FSMFeedback.feedback.set()
    # save message id and user uid
    async with state.proxy() as data:
        data['last_msg_id'] = callback_query.message.message_id
        data['user_uid'] = callback_query.from_user.id

    return

async def get_message(message: types.Message, state: FSMContext):
    # annotation
    admins: List[int]

    async with state.proxy() as data:
        # save obtained answer and user info in the FSM memory
        data['message'] = message.text
        data['username'] = message.from_user.username
        data['full_name'] = message.from_user.full_name
        # send feedback to the group/channel with specified ID
        await bot.send_message(
            chat_id=CHAT_ID,
            text=BotMessageText.feedback_request(details=data),
            parse_mode='HTML'
        )
        # add new feedback to the db
        await create_feedback(
            tg_uid=data['user_uid'],
            username=data['username'],
            message=data['message']
        )
        # remove reply_markup
        await bot.edit_message_reply_markup(
            chat_id=data['user_uid'],
            message_id=data['last_msg_id'],
            reply_markup=None
        )
        # send the "success message"
        bot_message = await bot.send_message(
            chat_id=data['user_uid'],
            text=BotMessageText.confirm_feedback_success.value,
            parse_mode='HTML'
        )
        # update message id
        data['last_msg_id'] = bot_message.message_id
        # set pause (give time to read)
        await asyncio.sleep(4)
        # get admins
        admins = await get_cache(key=CacheKeys.admins.value)
        # move back to menu
        await bot.send_message(
            chat_id=data['user_uid'],
            text=BotMessageText.menu_desc(),
            parse_mode='HTML',
            reply_markup=main_menu_admin if (
                    data['user_uid'] in admins or data['user_uid'] == MASTER_ADMIN
            ) else main_menu_client
        )
    # exit FSM
    await state.finish()

    return

def register_handlers(dp: Dispatcher) -> None:
    dp.register_callback_query_handler(
        leave_feedback,
        lambda x: x.data and x.data == CallbackData.leave_feedback.value,
        state='*'
    )
    dp.register_message_handler(
        get_message,
        state=FSMFeedback.feedback
    )

    return


import time

# some list
l = [7, 6, 1, 5, 4, 3]

def bubble(l):
    for i in range(len(l)):
        for j in range(i, len(l)):
            if l[i] > l[j]:
                l[i], l[j] = l[j], l[i]
    return l

s = time.time()
sorted(l)
print('built-in sorted', time.time() - s)

s = time.time()
bubble(l)
print('bubble sort', time.time() - s)

Data Structures & Algorithms/divide&conquer.py


#!/usr/bin/env python
# Testing commit permissions

from cStringIO import StringIO
from glob import glob
from gzip import GzipFile
from hashlib import md5
from multiprocessing import Pool
from optparse import OptionParser
from os.path import basename, join, splitext
import sys
import tarfile

def anonymize_update(tarfilename, destination_directory, check_destination):
    # extract the router ID from file name and create an MD5 hash
    router_id = basename(tarfilename).split('_', 1)[0]
    m = md5()
    m.update(router_id)
    hashed_router_id = m.hexdigest()

    output_pat = '%s_%s*.tar' \
            % (hashed_router_id,
               '_'.join(splitext(basename(tarfilename))[0].split('_', 3)[1:3]))
    existing_filenames = glob(join(destination_directory, output_pat))
    if len(existing_filenames) > 0:
        assert len(existing_filenames) == 1
        if check_destination:
            true_sum = splitext(
                    basename(existing_filenames[0]))[0].split('_')[3]
            hasher = md5()
            hasher.update(open(existing_filenames[0], 'r').read())
            if hasher.hexdigest() == true_sum:
                sys.stdout.write('_')
                sys.stdout.flush()
                return
            else:
                sys.stdout.write('!')
                sys.stdout.flush()
        else:
            sys.stdout.write('_')
            sys.stdout.flush()
            return
    try:
        true_sum = splitext(basename(tarfilename))[0].split('_')[3]
    except IndexError:
        true_sum = None
    if true_sum is not None:
        hasher = md5()
        hasher.update(open(tarfilename, 'r').read())
        if hasher.hexdigest() != true_sum:
            sys.stdout.write('?')
            sys.stdout.flush()
            return
    sys.stdout.write('.')
    sys.stdout.flush()

    # Read a tar file and output an anonymized tar file
    tarball = tarfile.open(tarfilename, 'r')
    outfile = StringIO()
    outtarball = tarfile.open(fileobj=outfile, mode='w')
    for tarmember in tarball:
        # get the .gz file
        tarhandle = tarball.extractfile(tarmember.name)
        logfile = GzipFile(fileobj=tarhandle)
        outtarbuffer = StringIO()
        outtarhandle = GzipFile(mode='w', fileobj=outtarbuffer)
        unanonymized = False
        for line in logfile:
            if line.startswith('UNANONYMIZED'):
                unanonymized = True
                break
            outtarhandle.write(line.replace(router_id, hashed_router_id))
        if unanonymized:
            continue

        # write compressed lines to the .gz file
        outtarhandle.close()
        outtarsize = outtarbuffer.tell() # size of the final buffer
        outtarbuffer.seek(0)

        # anonymize the router id from the gzip file as well
        outgzname = '%s-%s' % (hashed_router_id,
                               tarmember.name.split('-', 1)[1])
        outtarmember = tarfile.TarInfo(name=outgzname)
        outtarmember.size = outtarsize
        outtarmember.mtime = tarmember.mtime
        outtarmember.mode = tarmember.mode
        outtarmember.type = tarmember.type

        # add anonymized .gz file to tarball
        outtarball.addfile(outtarmember, outtarbuffer)
    outtarball.close()

    # Change the md5sum in the name of the file
    m = md5()
    m.update(outfile.getvalue())
    new_md5checksum = m.hexdigest()
    output_tarfilename = '%s_%s_%s.tar' \
            % (hashed_router_id,
               '_'.join(splitext(basename(tarfilename))[0].split('_', 3)[1:3]),
               new_md5checksum)
    output_path = join(destination_directory, output_tarfilename)
    open(output_path, 'w').write(outfile.getvalue())
    return output_path

def parse_args():
    usage = 'usage: %prog [options] source_directory anonymized_directory'
    parser = OptionParser(usage=usage)
    parser.add_option('-w', '--workers', type='int', action='store',
                      dest='workers', default=4,
                      help='Maximum number of worker threads to use')
    parser.add_option('-c', '--check-destination-sums', action='store_true',
                      dest='check_destination', default=False,
                      help='Verify destination checksums before skipping')
    options, args = parser.parse_args()
    if len(args) != 2:
        parser.error('Missing required option')
    mandatory = { 'source_directory': args[0],
                  'anonymized_directory': args[1] }
    return options, mandatory

def main():
    (options, args) = parse_args()
    pool = Pool(processes=options.workers)
    results = []
    for tarname in glob(join(args['source_directory'], '*.tar')):
        pool.apply_async(anonymize_update,
                         (tarname,
                          args['anonymized_directory'],
                          options.check_destination))
    pool.close()
    pool.join()
    print

if __name__ == '__main__':
    main()


import ssl
import socket
import contextlib

from puppy.simple.http import HTTP

from puppy.http.types import Request
from puppy.http.utilities import has_header, pop_header, set_header, split_url
from puppy.http.constants import GET, POST, COOKIE, SET_COOKIE, HOST, INTEGER

try:
    # Python 2 quote and unquote
    from urllib import quote, unquote
except:
    # Python 3 quote and unquote
    from urllib.parse import quote_from_bytes as quote, unquote_to_bytes as unquote

# Schema constants
SCHEMA_HTTP = b"http"
SCHEMA_HTTPS = b"https"
SCHEMA_MAPPING = {SCHEMA_HTTP: 80, SCHEMA_HTTPS: 443}

class HTTPClient(object):

    def __init__(self, implementation=HTTP):
        # Client variables
        self.implementation = implementation

        # State variables
        self.cookies = dict()
        self.headers = dict()
        self.history = list()

    def _update_request(self, request):
        # Check if cookies are defined
        if not self.cookies:
            return request

        # Create cookies header
        cookies = b"; ".join([
            # Format as name=value
            b"%s=%s" % (quote(name).encode(), quote(value).encode())
            # For each cookie in the jar
            for name, value in self.cookies.items()
        ])

        # Set cookies header
        set_header(request.headers, COOKIE, cookies)

        # Return the request
        return request

    def _update_response(self, response):
        # Loop over all response cookie headers
        while has_header(response.headers, SET_COOKIE):
            # Pop one set-cookie header
            header = pop_header(response.headers, SET_COOKIE)

            # Check if semicolon exists
            if b";" in header:
                # Split by semicolon
                cookie, _ = header.split(b";", 1)
            else:
                # Take as is
                cookie = header

            # Make sure '=' exists
            if b"=" not in cookie:
                continue

            # Split and decode
            name, value = cookie.split(b"=", 1)
            name, value = unquote(name.decode()), unquote(value.decode())

            # Update cookie jar
            self.cookies[name] = value

        # Return the response
        return response

    @contextlib.contextmanager
    def _create_connection(self, host, port, tls=False):
        # Create a socket
        connection = socket.socket()
        connection.connect((host, port))

        # Wrap socket if needed
        if tls:
            # Wrap connection with SSL
            context = ssl.create_default_context()

            # Disable certificate check
            context.check_hostname = False
            context.verify_mode = ssl.CERT_NONE

            # Wrap connection using context
            connection = context.wrap_socket(connection, server_hostname=host)

        try:
            # Yield the socket
            yield connection
        finally:
            # Close the connection
            connection.close()

    def get(self, url, *args, **kwargs):
        return self.request(GET, url, *args, **kwargs)

    def post(self, url, *args, **kwargs):
        return self.request(POST, url, *args, **kwargs)

    def request(self, method, url, headers=None, body=None):
        # Create header list
        headers = headers or list()

        # Parse URL using parser
        schema, host, port, path = split_url(url)

        # Append host header
        headers.append((HOST, host if not port else host + b":" + INTEGER % port))

        # Make sure port is defined
        schema = schema or SCHEMA_HTTP
        port = port or SCHEMA_MAPPING[schema]

        # Create the interface
        interface = self.implementation()

        # Create request object
        request = Request(method, path or b"/", headers, body)
        request = self._update_request(request)

        # Get interface for request by address and update host
        with self._create_connection(host, port, schema == SCHEMA_HTTPS) as connection:
            # Send request and receive response
            interface.transmit_request(connection, request)
            response = interface.receive_response(connection)

        # Update the cookies
        response = self._update_response(response)

        # Add new history item
        self.history.append((request, response))

        # Return response
        return response


# 58. Length of Last Word
# Given a string s consisting of words and spaces, return the length of the last word in the string.

# A word is a maximal
# substring
#  consisting of non-space characters only.

# Example 1:

# Input: s = "Hello World"
# Output: 5
# Explanation: The last word is "World" with length 5.
# Example 2:

# Input: s = "   fly me   to   the moon  "
# Output: 4
# Explanation: The last word is "moon" with length 4.

# Solution -->
class Solution:
    def lengthOfLastWord(self, s: str) -> int:
        sp = s.split()
        l  = len(sp)
        c = 0
        for i in sp[len(sp)-1]:
            c += 1

        return c


#!/usr/bin/env python3.6
# -*- coding=utf-8 -*-

from pecan.lang.ir import *

from pecan.lang.ir_transformer import IRTransformer

class IRSubstitution(IRTransformer):
    def __init__(self, subs):
        super().__init__()
        self.subs = subs

    def transform_str(self, original_str):
        return self.substitute_identifier(None, original_str)

    def substitute_identifier(self, node, original_str):
        if original_str in self.subs:
            if type(self.subs[original_str]) is PralineString:
                return self.subs[original_str].get_value()
            else:
                raise Exception('Cannot substitute a non-string for an identifier in "{}"; subs: {}'.format(node, self.subs))
        else:
            return original_str

    def transform_VarRef(self, node):
        if node.var_name in self.subs:
            return self.subs[node.var_name]
        else:
            return node

    def transform_Call(self, node):
        new_args = [self.transform(arg) for arg in node.args]
        if node.name in self.subs:
            sub = self.subs[node.name]
            if isinstance(sub, PralineString):
                return Call(sub.get_value(), new_args).with_type(node.get_type())
            elif isinstance(sub, PralinePecanLiteral) and isinstance(sub.get_term(), Call):
                call = sub.get_term()
                return Call(call.name, call.args + new_args).with_type(node.get_type())
            else:
                raise Exception('Cannot substitute a non-string or non-call for an identifier in "{}"; subs: {}'.format(node, self.subs))
        else:
            return Call(node.name, new_args).with_type(node.get_type())

    def transform_NamedPred(self, node):
        new_name = self.substitute_identifier(node, node.name)
        new_args = [self.transform(arg) for arg in node.args]
        new_restrictions = {self.transform(var): self.transform(restriction) for var, restriction in node.arg_restrictions.items()}
        return NamedPred(new_name, new_args, new_restrictions, self.transform(node.body), restriction_env=node.restriction_env, body_evaluated=node.body_evaluated, arg_name_map=node.arg_name_map)


import sqlite3
import os

#File and path for database
db_folder = os.path.join(os.path.dirname(__file__), "db_user.db")

def item():
    data = []
    conn = sqlite3.connect(db_folder)
    sql = """
        SELECT item, name, category
        FROM item
        ORDER BY name
    """
    cursor = conn.execute(sql)
    rows = cursor.fetchall()

    for row in rows:
        record = {
            'item': row[0],
            'name': row[1],
            'category': row[2]
            }
        data.append(record)

    conn.close()
    return data

def find_item(item):
    data = []
    conn = sqlite3.connect(db_folder)
    sql = """
        SELECT item, name, category
        FROM item
        WHERE item=?
    """
    val = (item,)
    cursor = conn.execute(sql,val)
    rows = cursor.fetchone()

    record = {
        'item': rows[0],
        'name': rows[1],
        'category': rows[2]
        }
    data.append(record)

    conn.close()
    return data

def item_add(item,name,category):
    conn = sqlite3.connect(db_folder)
    sql = """
        INSERT INTO item(item,name,category)
        VALUES(?,?,?)
    """
    val = (item,name,category)
    cursor = conn.execute(sql, val)
    conn.commit()
    conn.close()
    return "Created successfully"

def remove_item(item):
    conn = sqlite3.connect(db_folder)
    sql = """
        DELETE from item
        WHERE item=?
    """
    val = (item,)
    cursor = conn.execute(sql, val)
    conn.commit()
    conn.close()
    return "Item removed successfully"

def item_update(item,name,category):
    conn = sqlite3.connect(db_folder)
    sql = """
        UPDATE item(item,name,category)
        SET name=?, category=?
        WHERE item=?
    """
    val = (item,name,category)
    cursor = conn.execute(sql, val)
    conn.commit()
    conn.close()
    return "Updated successfully"


import logging
import os

import click

from stactools.ga_dlcd import cog, stac

logger = logging.getLogger(__name__)

def create_gadlcd_command(cli: click.Group) -> click.Command:
    """Creates the gadlcd command line utility."""
    @cli.group(
        "ga-dlcd",
        short_help="Commands for working with GA DLCD data",
    )
    def gadlcd() -> None:
        pass

    @gadlcd.command(
        "create-collection",
        short_help="Creates a STAC collection from GA DLCD metadata",
    )
    @click.option(
        "-d",
        "--destination",
        required=True,
        help="The output directory for the STAC Collection json",
    )
    def create_collection_command(destination: str) -> None:
        """Creates a STAC Collection from gadlcd metadata

        Args:
            destination (str): Directory used to store the collection json
        Returns:
            Callable
        """
        output_path = os.path.join(destination, "collection.json")
        collection = stac.create_collection()
        collection.set_self_href(output_path)
        collection.normalize_hrefs(destination)
        collection.save()
        collection.validate()

    @gadlcd.command(
        "create-cog",
        short_help="Transform Geotiff to Cloud-Optimized Geotiff.",
    )
    @click.option(
        "-d",
        "--destination",
        required=True,
        help="The output directory for the COG",
    )
    @click.option(
        "-s",
        "--source",
        required=True,
        help="Path to an input GeoTiff",
    )
    def create_cog_command(destination: str, source: str) -> None:
        """Generate a COG from a GeoTiff. The COG will be saved in the desination directory.

        Args:
            destination (str): Local directory to save output COGs
            source (str): An input GA-DLCD GeoTiff
        """
        if not os.path.isdir(destination):
            raise IOError(f'Destination folder "{destination}" not found')

        if destination == source:
            output_path = os.path.join(
                destination,
                os.path.basename(source).replace(".tif", "_cog.tif"))
        else:
            output_path = os.path.join(destination, os.path.basename(source))

        cog.create_cog(source, output_path)

    @gadlcd.command(
        "create-item",
        short_help="Create a STAC item using JSONLD metadata and a COG",
    )
    @click.option(
        "-d",
        "--destination",
        required=True,
        help="The output directory for the STAC json",
    )
    @click.option("-c", "--cog", required=True, help="COG href")
    def create_item_command(destination: str, cog: str) -> None:
        """Generate a STAC item using the metadata, with an asset url as provided.

        Args:
            destination (str): Local directory to save the STAC Item json
            cog (str): location of a COG asset for the item
        """

        output_path = os.path.join(
            destination,
            os.path.basename(cog).replace(".tif", ".json"))

        item = stac.create_item(cog)
        item.set_self_href(output_path)
        item.make_asset_hrefs_relative()
        item.save_object()
        item.validate()

    @gadlcd.command(
        "populate-full-collection",
        short_help="Create a STAC Collection with all of the Items and Assets",
    )
    @click.option(
        "-d",
        "--destination",
        required=True,
        help="The output directory",
    )
    @click.option(
        "-s",
        "--source",
        required=True,
        help="Dataset directory",
    )
    def populate_full_collection(destination: str, source: str) -> None:
        """Convert the dataset to COGs, and build the STAC Items and Collection

        Args:
            destination (str): Local directory to save output COGs
            source (str): A directory containing the GA-DLCD dataset TIFFs
        """
        if not os.path.isdir(destination):
            raise IOError(f'Destination folder "{destination}" not found')
        if not os.path.isdir(source):
            raise IOError(f'Source folder "{source}" not found')

        collection = stac.create_collection()
        collection.normalize_hrefs(destination)
        collection.save(dest_href=destination)
        for tiff in os.listdir(source):
            if tiff.endswith(".tif"):
                logger.info(f"Processing {tiff}")
                cog_path = os.path.join(destination, tiff)
                cog.create_cog(os.path.join(source, tiff), cog_path)

                item = stac.create_item(cog_href=cog_path)
                collection.add_item(item)
                item_path = cog_path.replace(".tif", ".json")
                item.set_self_href(item_path)
                item.make_asset_hrefs_relative()
                item.validate()

        logger.info("Saving collection")
        collection.save(dest_href=destination)
        collection.validate()

    return gadlcd