import copy
import datetime
import json
import numbers
import os
import pickle
import tempfile
import types
import unittest

from lib.attributes import Attributes
from lib.database import Database

class AttributesTestCase(unittest.TestCase):
    def setUp(self):
        parentpath = (
            os.path.abspath(
                os.path.join(
                    os.path.dirname(os.path.realpath(__file__)),
                    os.pardir
                )
            )
        )
        manifestpath = os.path.join(parentpath, 'manifest.json')
        self.rawattributes = None
        with open(manifestpath, 'r') as file_:
            self.rawattributes = json.load(file_)['attributes']

        configpath = os.path.join(parentpath, 'config.json')
        self.rawsettings = None
        with open(configpath, 'r') as file_:
            contents = json.load(file_)
            self.rawsettings = contents['options']['datasource']
            self.rawgoptions = {
                'today': contents['options']['datasource']
            }

    def test_init(self):
        # Arrange
        expected = len(self.rawattributes)

        # Act
        attributes = Attributes(
            self.rawattributes, database=None, goptions=self.rawgoptions
        )

        # Assert
        self.assertEqual(expected, len(attributes.attributes))
        for attribute in attributes.attributes:
            self.assertNotEqual('', attribute.name)
            self.assertNotEqual('', attribute.initial)
            self.assertIsInstance(attribute.weight, numbers.Number)
            self.assertIsInstance(attribute.enabled, bool)
            self.assertIsInstance(attribute.essential, bool)
            self.assertIsInstance(attribute.persist, bool)
            self.assertIsInstance(attribute.dependencies, list)
            self.assertIsInstance(attribute.options, dict)
            self.assertIsInstance(attribute.reference, types.ModuleType)

    def test_pickling(self):
        # Arrange
        attributes = Attributes(
            self.rawattributes, database=None, goptions=self.rawgoptions
        )
        expected = len(attributes.attributes)

        # Act
        pickled = pickle.dumps(attributes)
        unpickled = pickle.loads(pickled)

        # Assert
        self.assertIsInstance(unpickled, Attributes)
        self.assertEqual(expected, len(unpickled.attributes))
        for attribute in unpickled.attributes:
            self.assertNotEqual('', attribute.name)
            self.assertNotEqual('', attribute.initial)
            self.assertIsInstance(attribute.weight, numbers.Number)
            self.assertIsInstance(attribute.enabled, bool)
            self.assertIsInstance(attribute.essential, bool)
            self.assertIsInstance(attribute.persist, bool)
            self.assertIsInstance(attribute.dependencies, list)
            self.assertIsInstance(attribute.options, dict)
            self.assertIsInstance(attribute.reference, types.ModuleType)

    def test_keystring(self):
        # Arrange
        keystring = 'DuAlIcH'

        # Act
        attributes = Attributes(
            self.rawattributes, database=None, keystring=keystring,
            goptions=self.rawgoptions
        )

        # Assert
        for attribute in attributes.attributes:
            index = str.find(keystring.lower(), attribute.initial)
            if index == -1:
                self.assertFalse(attribute.enabled)
            else:
                self.assertTrue(attribute.enabled)
                if keystring[index].isupper():
                    self.assertTrue(attribute.persist)
                else:
                    self.assertFalse(attribute.persist)

    def test_is_persitence_enabled(self):
        # Arrange
        keystring = 'dualmichs'

        # Act
        attributes = Attributes(
            self.rawattributes, database=None, keystring=keystring,
            goptions=self.rawgoptions
        )

        # Assert
        self.assertFalse(attributes.is_persistence_enabled)

        # Arrange
        keystring = 'dualMichs'

        # Act
        attributes = Attributes(
            self.rawattributes, database=None, keystring=keystring
        )

        # Assert
        self.assertTrue(attributes.is_persistence_enabled)

    def test_init_repository(self):
        with tempfile.TemporaryDirectory() as directory:
            # Arrange
            project_id = 10868464
            repository_path = os.path.join(directory, str(project_id))
            expected = os.path.join(
                directory, str(project_id), 'squib'
            )

            # Act
            attributes = Attributes(
                self.rawattributes, database=Database(self.rawsettings),
                goptions=self.rawgoptions
            )
            try:
                attributes.database.connect()
                actual = attributes._init_repository(
                    project_id, repository_path
                )

                # Assert
                self.assertTrue(len(os.listdir(repository_path)) > 0)
                self.assertTrue(expected in actual)
            finally:
                attributes.database.disconnect()

    def test_cleanup(self):
        with tempfile.TemporaryDirectory() as directory:
            # Arrange
            project_id = 10868464
            repository_home = os.path.join(directory, str(project_id))
            attributes = Attributes(
                self.rawattributes, database=Database(self.rawsettings),
                goptions=self.rawgoptions
            )
            try:
                attributes.database.connect()
                attributes._init_repository(project_id, repository_home)

                # Act
                attributes._cleanup(repository_home)

                # Assert
                self.assertFalse(os.path.exists(repository_home))
            finally:
                attributes.database.disconnect()

    def test_run_timeout(self):
        with tempfile.TemporaryDirectory() as directory:
            # Arrange
            project_id = 10868464
            repository_path = directory
            rawattributes = copy.deepcopy(self.rawattributes)
            for attribute in rawattributes:
                if 'architecture' in attribute['name']:
                    attribute['options']['timeout'] = '1S'  # Sabotage
            expected = (0, {'architecture': None})

            # Act
            attributes = Attributes(
                rawattributes,
                database=Database(self.rawsettings),
                keystring='a',
                goptions=self.rawgoptions
            )
            try:
                attributes.database.connect()
                actual = attributes.run(project_id, repository_path)

                # Assert
                self.assertEqual(expected, actual)
            finally:
                attributes.database.disconnect()

    def test_score(self):
        # Global Arrange
        attributes = Attributes(
            self.rawattributes, database=None, goptions=self.rawgoptions
        )

        # Arrange
        rresults = {
            'architecture': 9.00,
            'community': 9.00,
            'continuous_integration': True,
            'documentation': 9.00,
            'history': 9.00,
            'license': True,
            'management': 9.00,
            'state': 'active',
            'unit_test': 9.00,
        }
        expected = 100.00

        # Act
        actual = attributes.score(rresults)

        # Assert
        self.assertEqual(expected, actual)

        # Arrange
        rresults = {
            'architecture': 9.00,
            'community': 9.00,
            'continuous_integration': True,
            'documentation': 0,
            'history': 9.00,
            'license': True,
            'management': 9.00,
            'state': 'active',
            'unit_test': 9.00,
        }
        expected = 80.00

        # Act
        actual = attributes.score(rresults)

        # Assert
        self.assertEqual(expected, actual)

        # Arrange
        rresults = {
            'architecture': 9.00,
            'community': 9.00,
            'continuous_integration': True,
            'documentation': 9.00,
            'history': 9.00,
            'license': False,
            'management': 9.00,
            'state': 'active',
            'unit_test': 9.00,
        }
        expected = 0

        # Act
        actual = attributes.score(rresults)

        # Assert
        self.assertEqual(expected, actual)

    def test_requires_source(self):
        # Arrange
        keystring = 'mchs'

        # Act
        attributes = Attributes(
            self.rawattributes, database=None, keystring=keystring,
            goptions=self.rawgoptions
        )

        # Assert
        self.assertFalse(attributes.requires_source)

        # Arrange
        keystring = 'dualmichs'

        # Act
        attributes = Attributes(
            self.rawattributes, database=None, keystring=keystring
        )

        # Assert
        self.assertTrue(attributes.requires_source)


import json
import os
import django

# Import necessary modules and classes
from apps.llm.agents.agent_factory import AgentFactory
from apps.llm.agents.callbacks import AsyncStreamingCallbackHandler
from apps.llm.conversation_messages import ConversationMessageRepository

# Set up Django settings
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'project.settings')
django.setup()

# Import the WebSocket consumer class
from channels.generic.websocket import AsyncWebsocketConsumer
from langchain.agents import AgentExecutor

from apps.llm.models import SenderMessage

# Define a WebSocket consumer for chat functionality
class ConversationConsumer(AsyncWebsocketConsumer):
    agent: AgentExecutor

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.agent_factory = AgentFactory()
        self.chat_message_repository = ConversationMessageRepository()

    async def connect(self):
        # Get the chat_id from the URL route
        chat_id = self.scope['url_route']['kwargs'].get('chat_id')

        # Create an agent with the specified parameters
        self.agent = await self.agent_factory.create_agent(
            tool_names=["llm-math"],
            chat_id=chat_id,
            streaming=True,
            callback_handlers=[AsyncStreamingCallbackHandler(self)],
        )

        await self.accept()

    async def disconnect(self, close_code):
        pass

    async def receive(self, text_data):
        text_data_json = json.loads(text_data)
        message = text_data_json['message']
        chat_id = text_data_json['chat_id']

        # Send the user's message to the agent for processing
        response = await self.message_agent(message, chat_id)

        # Send the agent's response back to the WebSocket client
        await self.send(text_data=json.dumps({'message': response, 'type': 'answer'}))

    async def message_agent(self, message: str, chat_id: str):
        # Save the user's message to the repository
        await self.chat_message_repository.save_message(message=message, sender=SenderMessage.USER.value,
                                                        chat_id=chat_id)

        # Use the agent to process the user's message and get a response
        response = await self.agent.arun(message)

        # Save the agent's response to the repository
        await self.chat_message_repository.save_message(message=response, sender=SenderMessage.AI.value,
                                                        chat_id=chat_id)

        return response

    def my_callback(self, message):
        print("Callback received:", message)


import numpy as np

class Node:
    def __init__(self, name):
        self.name = name
        self.connections = []
        self.visited = False

def connect(node1, node2, weight):
    node1.connections.append({"node": node2, "weight": weight})
    node2.connections.append({"node": node1, "weight": weight})

def BFS(node):
    """Visits and prints all nodes in a graph"""
    q = [node]
    node.visited = True
    while len(q) > 0:
        cur = q.pop(0) # remove q[0] from q and put it in cur
        print(cur.name)
        for con in cur.connections:
            if not con["node"].visited:
                q.append(con["node"])
                con["node"].visited = True

def get_all_nodes(node):
    '''Return a list of the nodes in the graph of nodes connected to node
    (N.B., the nodes can be indirectly connected as well)'''
    q = [node]
    nodes = [node]
    node.visited = True
    while len(q) > 0:
        cur = q.pop(0)  # remove q[0] from q and put it in cur
        for con in cur.connections:
            if not con["node"].visited:
                q.append(con["node"])
                nodes.append(con["node"])
                con["node"].visited = True

    for n in nodes:
        n.visited = False

    return nodes

def unvisit_all(node):
    '''Change all n.visited to False in all the nodes in the graph of nodes
    connected to node. Use BFS to find all the nodes'''
    q = [node]
    node.visited = False
    while len(q) > 0:
        cur = q.pop(0) # remove q[0] from q and put it in cur
        for con in cur.connections:
            if con["node"].visited:
                q.append(con["node"])
                con["node"].visited = False

def DFS_rec(node):
    '''Print out the names of all nodes connected to node using a
    recursive version of DFS'''
    node.visited = True
    print(node.name)

    for con in node.connections:
        if not con["node"].visited:
            DFS_rec(con["node"])

def DFS(node):
    '''Print out the names of all nodes connected to node using a non-recursive
    version of DFS. Make it so that the nodes are printed in the same order
    as in DFS_rec'''

    q = [node]
    node.visited = True
    while (len(q) > 0):
        cur = q.pop(-1)
        print(cur.name)

        for con in reversed(cur.connections):
            if not con["node"].visited:

                q.append(con["node"])
                con["node"].visited = True

def dijsktra_slowish(node):
    '''Implement Dijkstra's algorithm as discussed in class (i.e., you don't
    need to use a priority queue'''
    S = [node]
    d = {node.name: 0}

    unexplored = get_all_nodes(node)
    unexplored.remove(node)

    while len(unexplored) > 0:
        min = ["no node found", float("inf")]
        for node in S:
            for con in node.connections:
                if con["node"] not in S:
                    if con["weight"] + d[node.name] < min[1]:
                        min = [con["node"], con["weight"] + d[node.name]]

        S.append(min[0])
        d[min[0].name] = min[1]
        unexplored.remove(min[0])

    return d

if __name__ == '__main__':
    TO = Node("TO")
    NYC = Node("NYC")
    DC = Node("DC")
    CDMX = Node("CDMX")
    SF = Node("SF")

    connect(TO, NYC, 3)
    connect(TO, SF, 6)
    connect(TO, CDMX, 7)
    connect(NYC, DC, 2)
    connect(SF, DC, 5)

    L = get_all_nodes(TO)
    print("\n")

    BFS(TO)
    print("\n")

    unvisit_all(TO)
    DFS_rec(TO)

    unvisit_all(TO)
    print("\n")

    DFS(TO)

    print("\n")

    unvisit_all(TO)
    print(dijsktra_slowish(TO))


import setuptools

with open("README.md", "r") as fh:
    long_description = fh.read()

setuptools.setup(
    name="leglib",  # Replace with your own username
    version="0.0.4",
    author="Joe Legner",
    author_email="joelegner@gmail.com",
    description="My personal Python shared package with lots of weird stuff.",
    long_description=long_description,
    long_description_content_type="text/markdown",
    url="https://github.com/joelegner/leglib",
    packages=setuptools.find_packages(),
    classifiers=[
        "Programming Language :: Python :: 3",
        "License :: OSI Approved :: MIT License",
        "Operating System :: OS Independent",
    ],
    python_requires='>=3.6',
)


def calc(a, b):
    sum = a + b
    difference = a - b
    multiplication = a * b
    division = a / b

    my_list = [sum, difference, multiplication, division]

    for x in range(0, len(my_list)):
        sum = sum + my_list[x]

    print(sum)

calc(3,4)


from bs4 import BeautifulSoup
from multiprocessing import Pool
import requests
import json
base_url = 'http://maoyan.com/board/4?offset='

headers = {
	'user-Agent':'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.186 Safari/537.36'
}
def getHTML(base_url,offset):
	url = base_url + str(offset)
	try:
		response = requests.get(url,headers = headers)
	except requests.HTTPError as e:
		return None
	else:
		return response.text

def parseHTML(html):
	soup = BeautifulSoup(html,'lxml')
	dd = soup.find_all('dd')
	for item in dd:
		yield {
			'index':item.select('.board-index')[0].get_text(),
			'name':item.select('.name')[0].get_text(),
			'star':item.select('.star')[0].get_text().strip(),
			'releasetime':item.select('.releasetime')[0].get_text()[5:],
			'score':item.select('.score')[0].get_text()
		}

def write_to_file(content):
	with open('../Result/maoyan_result.txt','a',encoding='utf-8') as f:
		f.write(json.dumps(content,ensure_ascii=False) + '\n')
		f.close()

def main(offset):
	for item in parseHTML(getHTML(base_url,offset)):
		write_to_file(item)

if __name__ == "__main__":
	for i in range(0,100,10):
		main(i)
	# pool = Pool()
	# pool.map(main,[i for i in range(0,100,10)])


from fastapi import APIRouter, Response, status
from pydantic import BaseModel

router = APIRouter()

class PostRequest(BaseModel):
    name: str

HELLO = 'Hi'

@router.get('')
def get_test():
    return {
        'msg': f'{HELLO} World!'
    }

@router.get('/{who}')
async def get_test_for_somebody(who: str):
    return {
        'msg': f'{HELLO} {who}!'
    }

@router.post('')
def post_test(body: PostRequest, response: Response):
    response.status_code = status.HTTP_202_ACCEPTED
    return {
        'msg': f'{HELLO} {body.name}!'
    }


import random
import pandas as pd

data = pd.read_csv('data/polyvore/processed_data/compatible_sets.csv')
data.head()

new_data = data.groupby('set_id').agg({'item_index': lambda x: list(x), 'categoryid': lambda x: list(x), 'type': lambda x: list(x)})
new_data['label'] = 1
new_data.reset_index(inplace=True)
new_data.head()

aligned_data = pd.DataFrame(columns=['set_id', 'item_index', 'categoryid', 'type', 'label'])
for i in range(len(new_data)):
    if new_data.iloc[i]['type'] == ['top', 'bottom']:
        aligned_data.at[i, 'set_id'] = new_data.iloc[i]['set_id']
        aligned_data.at[i, 'item_index'] = new_data.iloc[i]['item_index']
        aligned_data.at[i, 'categoryid'] = new_data.iloc[i]['categoryid']
        aligned_data.at[i, 'type'] = new_data.iloc[i]['type']
        aligned_data.at[i, 'label'] = new_data.iloc[i]['label']
    elif new_data.iloc[i]['type'] == ['bottom', 'top']:
        aligned_data.at[i, 'set_id'] = new_data.iloc[i]['set_id']
        aligned_data.at[i, 'item_index'] = new_data.iloc[i]['item_index'][::-1]
        aligned_data.at[i, 'categoryid'] = new_data.iloc[i]['categoryid'][::-1]
        aligned_data.at[i, 'type'] = new_data.iloc[i]['type'][::-1]
        aligned_data.at[i, 'label'] = new_data.iloc[i]['label']
    else:
        pass

# for each row in new_data, make a new data frame with the top and bottom items on the same row with separate column named with type
aligned_data.head()
sorted_data = pd.DataFrame(columns=['top_index', 'bottom_index', 'top_categoryid', 'bottom_categoryid', 'label'])
for i in range(len(aligned_data)):
    set_id = aligned_data.iloc[i]['set_id']
    top_index = aligned_data.iloc[i]['item_index'][0]
    bottom_index = aligned_data.iloc[i]['item_index'][1]
    top_categoryid = aligned_data.iloc[i]['categoryid'][0]
    bottom_categoryid = aligned_data.iloc[i]['categoryid'][1]
    label = aligned_data.iloc[i]['label']

    sorted_data.at[i, 'top_index'] = f'{set_id}_{top_index}'
    sorted_data.at[i, 'bottom_index'] = f'{set_id}_{bottom_index}'
    sorted_data.at[i, 'top_categoryid'] = top_categoryid
    sorted_data.at[i, 'bottom_categoryid'] = bottom_categoryid
    sorted_data.at[i, 'label'] = label

sorted_data.head()

# shuffle top
shuffled_index = list(range(len(sorted_data)))
random.shuffle(shuffled_index)

shuffled_data_top = pd.DataFrame(columns=['top_index', 'bottom_index', 'top_categoryid', 'bottom_categoryid', 'label'])
for i in range(0, len(sorted_data), 2):
    j = shuffled_index[i]
    set_id = aligned_data.iloc[i]['set_id']
    top_index = sorted_data.iloc[i]['top_index']
    top_categoryid = sorted_data.iloc[i]['top_categoryid']
    bottom_index = sorted_data.iloc[j]['bottom_index']
    bottom_categoryid = sorted_data.iloc[j]['bottom_categoryid']
    label = 0

    shuffled_data_top.at[i, 'top_index'] = top_index
    shuffled_data_top.at[i, 'bottom_index'] = bottom_index
    shuffled_data_top.at[i, 'top_categoryid'] = top_categoryid
    shuffled_data_top.at[i, 'bottom_categoryid'] = bottom_categoryid
    shuffled_data_top.at[i, 'label'] = label

# shuffle bottom
shuffled_index = list(range(len(sorted_data)))
random.shuffle(shuffled_index)

shuffled_data_bottom = pd.DataFrame(columns=['top_index', 'bottom_index', 'top_categoryid', 'bottom_categoryid', 'label'])
for i in range(0, len(sorted_data), 2):
    j = shuffled_index[i]
    set_id = aligned_data.iloc[i]['set_id']
    top_index = sorted_data.iloc[j]['top_index']
    top_categoryid = sorted_data.iloc[j]['top_categoryid']
    bottom_index = sorted_data.iloc[i]['bottom_index']
    bottom_categoryid = sorted_data.iloc[i]['bottom_categoryid']
    label = 0

    shuffled_data_bottom.at[i, 'top_index'] = top_index
    shuffled_data_bottom.at[i, 'bottom_index'] = bottom_index
    shuffled_data_bottom.at[i, 'top_categoryid'] = top_categoryid
    shuffled_data_bottom.at[i, 'bottom_categoryid'] = bottom_categoryid
    shuffled_data_bottom.at[i, 'label'] = label

# combine the shuffled data
combined_data = pd.concat([sorted_data, shuffled_data_top, shuffled_data_bottom], ignore_index=True)
combined_data.to_csv('data/polyvore/processed_data/combined_data.csv', index=False)


import tensorflow as tf
import tensorflow.keras as keras
from replay_memory.ReplayBuffer import ReplayBuffer
from utils.networks import ActorNetwork, CriticNetwork

## Actor-critic networks parameters :

# actor learning rate
alpha = 0.001

# critic learning rate
beta = 0.002

## DDPG algorithms paramters

# discount factor
gamma = 0.99

# target netwroks soft update factor
tau = 0.005

# replay buffer max memory size
max_size = 10**6

# exploration noise factor
noise_factor = 0.1

# training batch size
batch_size = 64

## DDPG agent class
class DDPGAgent:
    def __init__(self, env, input_dims):
        self.gamma = gamma
        self.tau = tau
        self.batch_size = batch_size
        self.noise_factor = noise_factor

        self.env = env
        self.n_actions = env.action_space.shape[0]
        self.max_action = env.action_space.high[0]
        self.min_action = env.action_space.low[0]

        self.memory = ReplayBuffer(max_size, input_dims, self.n_actions)

        self._initialize_networks(self.n_actions)
        self.update_parameters(tau=1)

    # Choose action based on actor network
    # Add exploration noise if in traning mode
    def choose_action(self, state, evaluate=False):
        state = tf.convert_to_tensor([state], dtype=tf.float32)
        actions = self.actor(state)
        if not evaluate:
            actions += tf.random.normal(shape=[self.n_actions], mean=0, stddev=self.noise_factor)
        actions = tf.clip_by_value(actions, self.min_action, self.max_action)
        return actions[0]

    def remember(self, state, action, reward, new_state, done):
        self.memory.store_transition(state, action, reward, new_state, done)

    # Main DDPG algorithms learning process
    def learn(self):
          if self.memory.counter < self.batch_size:
              return

          # Sample batch size of experiences from replay buffer
          states, actions, rewards, new_states, dones = self.memory.sample(self.batch_size)
          states = tf.convert_to_tensor(states, dtype=tf.float32)
          actions = tf.convert_to_tensor(actions, dtype=tf.float32)
          rewards = tf.convert_to_tensor(rewards, dtype=tf.float32)
          new_states = tf.convert_to_tensor(new_states, dtype=tf.float32)

          # Calculate critic network loss
          with tf.GradientTape() as tape:
              target_actions = self.target_actor(new_states)
              new_critic_value = tf.squeeze(self.target_critic(new_states, target_actions), 1)
              critic_value = tf.squeeze(self.critic(states, actions), 1)
              target = rewards + self.gamma * new_critic_value * (1 - dones)
              critic_loss = tf.keras.losses.MSE(target, critic_value)

          # Apply gradient decente with the calculated critic loss
          critic_network_gradient = tape.gradient(critic_loss, self.critic.trainable_variables)
          self.critic.optimizer.apply_gradients(zip(
              critic_network_gradient, self.critic.trainable_variables
          ))

          # Calculate actor network loss
          with tf.GradientTape() as tape:
              new_actions = self.actor(states)
              actor_loss = - self.critic(states, new_actions)
              actor_loss = tf.math.reduce_mean(actor_loss)

          # Apply gradient decente with the calculated actor loss
          actor_network_gradient = tape.gradient(actor_loss, self.actor.trainable_variables)
          self.actor.optimizer.apply_gradients(zip(
                  actor_network_gradient, self.actor.trainable_variables
              ))

          # Update actor/critic target networks
          self.update_parameters()

    # Update actor/critic target networks parameters with soft update rule
    def update_parameters(self, tau=None):
        if tau is None:
            tau = self.tau

        weights = []
        targets = self.target_actor.weights
        for i, weight in enumerate(self.actor.weights):
            weights.append(tau * weight + (1 - tau) * targets[i])
        self.target_actor.set_weights(weights)

        weights = []
        targets = self.target_critic.weights
        for i, weight in enumerate(self.critic.weights):
            weights.append(tau * weight + (1 - tau) * targets[i])
        self.target_critic.set_weights(weights)

    def save_models(self):
        print("---- saving models ----")
        self.actor.save_weights(self.actor.checkpoints_file)
        self.critic.save_weights(self.critic.checkpoints_file)
        self.target_actor.save_weights(self.target_actor.checkpoints_file)
        self.target_critic.save_weights(self.target_critic.checkpoints_file)

    def load_models(self):
        print("---- loading models ----")
        self.actor.load_weights(self.actor.checkpoints_file)
        self.critic.load_weights(self.critic.checkpoints_file)
        self.target_actor.load_weights(self.target_actor.checkpoints_file)
        self.target_critic.load_weights(self.target_critic.checkpoints_file)

    def _initialize_networks(self, n_actions):
        model = "ddpg"
        self.actor = ActorNetwork(n_actions, name="actor", model=model)
        self.critic = CriticNetwork(name="critic", model=model)
        self.target_actor = ActorNetwork(n_actions, name="target_actor", model=model)
        self.target_critic = CriticNetwork(name="target_critic", model=model)

        self.actor.compile(keras.optimizers.Adam(learning_rate=alpha))
        self.critic.compile(keras.optimizers.Adam(learning_rate=beta))
        self.target_actor.compile(keras.optimizers.Adam(learning_rate=alpha))
        self.target_critic.compile(keras.optimizers.Adam(learning_rate=beta))


# -*- coding: utf-8 -*-
"""
Created on Sun Feb 14 12:35:08 2021

@author: lefti
"""

#importing the libraries

import tensorflow as tf
from keras.preprocessing.image import ImageDataGenerator

# Preprocessing the data
train_datagen = ImageDataGenerator(rescale=1./255,
                                   shear_range=0.2,
                                   zoom_range=0.2,
                                   horizontal_flip=True)

training_set = train_datagen.flow_from_directory( 'dataset/Training_set',
                                                    target_size=(256,256),
                                                    batch_size =32,
                                                    class_mode='binary')
test_datagen =ImageDataGenerator(rescale=1./255)
test_set = test_datagen.flow_from_directory('dataset/test',
                                                  target_size= (256,256),
                                                  batch_size =32,
                                                  class_mode = 'binary'
                                                  )

#building the Convolutional neural network
#initialise the cnn
cnn = tf.keras.models.Sequential()
#adding the first and input layer
cnn.add(tf.keras.layers.Conv2D(filters=32,
                               kernel_size=4,
                               activation='relu',
                               input_shape =[256,256,3]))
#adding the pooling to first layer
cnn.add(tf.keras.layers.MaxPool2D(pool_size=2,strides=2))

#add layer 2
cnn.add(tf.keras.layers.Conv2D(filters= 32,
                               kernel_size=4,
                               activation='relu'))
#add pooling to layer2
cnn.add(tf.keras.layers.MaxPool2D(pool_size=2,strides=2))

#flattening the layers
cnn.add(tf.keras.layers.Flatten())

#creating full connection
cnn.add(tf.keras.layers.Dense(units=150,activation='relu'))

#output layer
cnn.add(tf.keras.layers.Dense(units=1,activation='sigmoid'))

#compiling the cnn
cnn.compile(optimizer = 'adam', loss='binary_crossentropy',metrics=['accuracy'])

#training the cnn
cnn.fit(x=training_set,validation_data = test_set,epochs=32)

Deep Learning/CNN/Electric bus or cars classification/carbusclassification.py


#!/usr/bin/python3
# -*- coding: utf-8; tab-width: 4; indent-tabs-mode: t -*-

import os
import configparser
from core.util import FvpUtil
from core.peripheral import FvpPeripheral

class FvpVmEnvironment:

    class NetworkInfo:
        virtioNetworkType = None                    # str        "bridge", "nat", "route" => ""

    class PeripheralInfo:
        obj = None                                  # reference to FvpPeripheral object
        persistent = None                           # bool
        connectWhenStart = None                     # bool
        uiShortcut = None                           # bool

    def __init__(self):
        self.networkInfo = FvpVmEnvironment.NetworkInfo()
        self.peripheralInfoList = []

    def checkValid(self):
        """if FvpVmEnvironment object is invalid, raise exception"""

        validVirtioNetworkType = ["bridge", "nat", "route", ""]

        if self.networkInfo.virtioNetworkType is None or self.networkInfo.virtioNetworkType not in validVirtioNetworkType:
            raise Exception("virtioNetworkType is invalid")

        for p in self.peripheralInfoList:
            try:
                p.obj.checkValid()
            except Exception as e:
                raise Exception("peripheral \"%s\" is invalid, %s" % (p.obj.pName, e.strerror))

    def getVirtioNetworkType(self):
        return self.networkInfo.virtioNetworkType

    def addPeripheral(self, peripheralObj, persistent, connectWhenStart, uiShortcut):
        # forbid name duplication
        for pInfo in self.peripheralInfoList:
            assert pInfo.obj.pName != peripheralObj.pName

        # add data
        pInfo = self.PeripheralInfo()
        pInfo.obj = peripheralObj
        pInfo.persistent = persistent
        pInfo.connectWhenStart = connectWhenStart
        pInfo.uiShortcut = uiShortcut
        self.peripheralInfoList.append(pInfo)

    def getPeripheralList(self):
        return self.peripheralInfoList

#    def remove(self, peripheralName):
#        pass
#
#
#    def getObj(self, peripheralName):
#        return self.objDict[peripheralName]
#
#    def setPersistent(self, peripheralName, flag):
#        self.infoDict[peripheralName].persistent = flag
#
#    def isPersistent(self, peripheralName):
#        return self.infoDict[peripheralName].persistent
#
#    def setConnectWhenStart(self, peripheralName, flag):
#        self.infoDict[peripheralName].connectWhenStart = flag
#
#    def isConnectWhenStart(self, peripheralName):
#        return self.infoDict[peripheralName].connectWhenStart

    def load(self, vmDir):
        fileName = os.path.join(vmDir, "fqemu.env")

        # clear self
        self.networkInfo.virtioNetworkType = ""
        self.peripheralInfoList = []

        # check file
        if not os.path.exists(fileName):
            return

        # read from cfg file
        cfg = configparser.RawConfigParser()
        cfg.read(os.path.join(vmDir, "fqemu.env"))

        self.networkInfo.virtioNetworkType = cfg.get("networkInfo", "virtioNetworkType")

        for secName in cfg.sections():
            if secName.startswith("peripheral"):
                p = FvpVmEnvironment.PeripheralInfo()

                p.obj = FvpPeripheral()
                p.obj.pType = cfg.get(secName, "pType")
                p.obj.pName = cfg.get(secName, "pName")
                for (name, value) in cfg.items(secName):
                    if name.startswith("param_"):
                        p.obj.paramDict[name[6:]] = value

                p.connectWhenStart = bool(cfg.get(secName, "connectWhenStart"))
                p.uiShortcut = False                                                    # fixme
                p.persistent = True

                self.peripheralInfoList.append(p)

        # type conversion
        for p in self.peripheralInfoList:
            if p.obj.pType == "file":
                if "readonly" in p.obj.paramDict:
                    p.obj.paramDict["readonly"] = FvpUtil.str2bool(p.obj.paramDict["readonly"])
            elif p.obj.pType == "folder":
                if "readonly" in p.obj.paramDict:
                    p.obj.paramDict["readonly"] = FvpUtil.str2bool(p.obj.paramDict["readonly"])

    def save(self, vmDir):
        fileName = os.path.join(vmDir, "fqemu.env")

        # save config file
        cfg = configparser.RawConfigParser()

        # save network parameter
        cfg.add_section("networkInfo")
        cfg.set("networkInfo", "virtioNetworkType", self.networkInfo.virtioNetworkType)

        # save peripheral list
        i = 0
        for p in self.peripheralInfoList:
            if not p.persistent:
                continue
            secName = "peripheral%d" % (i)
            cfg.add_section(secName)
            cfg.set(secName, "pType", p.obj.pType)
            cfg.set(secName, "pName", p.obj.pName)
            for k, v in p.paramDict:
                cfg.set(secName, "param_%s" % (k), str(v))
            cfg.set(secName, "connectWhenStart", str(p.connectWhenStart))
            i = i + 1

        cfg.write(open(fileName, "w"))


"""
Class representing the samconfig.toml
"""

import logging
import os
from pathlib import Path
from typing import Any, Iterable

from samcli.lib.config.exceptions import FileParseException, SamConfigFileReadException, SamConfigVersionException
from samcli.lib.config.file_manager import FILE_MANAGER_MAPPER
from samcli.lib.config.version import SAM_CONFIG_VERSION, VERSION_KEY
from samcli.lib.telemetry.event import EventTracker

LOG = logging.getLogger(__name__)

DEFAULT_CONFIG_FILE_EXTENSION = ".toml"
DEFAULT_CONFIG_FILE = "samconfig"
DEFAULT_CONFIG_FILE_NAME = DEFAULT_CONFIG_FILE + DEFAULT_CONFIG_FILE_EXTENSION
DEFAULT_ENV = "default"
DEFAULT_GLOBAL_CMDNAME = "global"

class SamConfig:
    """
    Class to represent `samconfig` config options.
    """

    def __init__(self, config_dir, filename=None):
        """
        Initialize the class

        Parameters
        ----------
        config_dir : string
            Directory where the configuration file needs to be stored
        filename : string
            Optional. Name of the configuration file. It is recommended to stick with default so in the future we
            could automatically support auto-resolving multiple config files within same directory.
        """
        self.document = {}
        self.filepath = Path(config_dir, filename or self.get_default_file(config_dir=config_dir))
        file_extension = self.filepath.suffix
        self.file_manager = FILE_MANAGER_MAPPER.get(file_extension, None)
        if not self.file_manager:
            LOG.warning(
                f"The config file extension '{file_extension}' is not supported. "
                f"Supported formats are: [{'|'.join(FILE_MANAGER_MAPPER.keys())}]"
            )
            raise SamConfigFileReadException(
                f"The config file {self.filepath} uses an unsupported extension, and cannot be read."
            )
        self._read()
        EventTracker.track_event("SamConfigFileExtension", file_extension)

    def get_stage_configuration_names(self):
        if self.document:
            return [stage for stage, value in self.document.items() if isinstance(value, dict)]
        return []

    def get_all(self, cmd_names, section, env=DEFAULT_ENV):
        """
        Gets a value from the configuration file for the given environment, command and section

        Parameters
        ----------
        cmd_names : list(str)
            List of representing the entire command. Ex: ["local", "generate-event", "s3", "put"]
        section : str
            Specific section within the command to look into. e.g. `parameters`
        env : str
            Optional, Name of the environment

        Returns
        -------
        dict
            Dictionary of configuration options in the file. None, if the config doesn't exist.

        Raises
        ------
        KeyError
            If the config file does *not* have the specific section
        """

        env = env or DEFAULT_ENV

        self.document = self._read()

        config_content = self.document.get(env, {})
        params = config_content.get(self.to_key(cmd_names), {}).get(section, {})
        if DEFAULT_GLOBAL_CMDNAME in config_content:
            global_params = config_content.get(DEFAULT_GLOBAL_CMDNAME, {}).get(section, {})
            global_params.update(params.copy())
            params = global_params.copy()
        return params

    def put(self, cmd_names, section, key, value, env=DEFAULT_ENV):
        """
        Writes the `key=value` under the given section. You have to call the `flush()` method after `put()` in
        order to write the values back to the config file. Otherwise they will be just saved in-memory, available
        for future access, but never saved back to the file.

        Parameters
        ----------
        cmd_names : list(str)
            List of representing the entire command. Ex: ["local", "generate-event", "s3", "put"]
        section : str
            Specific section within the command to look into. e.g. `parameters`
        key : str
            Key to write the data under
        value : Any
            Value to write. Could be any of the supported types.
        env : str
            Optional, Name of the environment
        """
        # Empty document prepare the initial structure.
        # self.document is a nested dict, we need to check each layer and add new tables, otherwise duplicated key
        # in parent layer will override the whole child layer
        cmd_name_key = self.to_key(cmd_names)
        env_content = self.document.get(env, {})
        cmd_content = env_content.get(cmd_name_key, {})
        param_content = cmd_content.get(section, {})
        if param_content:
            param_content.update({key: value})
        elif cmd_content:
            cmd_content.update({section: {key: value}})
        elif env_content:
            env_content.update({cmd_name_key: {section: {key: value}}})
        else:
            self.document.update({env: {cmd_name_key: {section: {key: value}}}})
        # If the value we want to add to samconfig already exist in global section, we don't put it again in
        # the special command section
        self._deduplicate_global_parameters(cmd_name_key, section, key, env)

    def put_comment(self, comment):
        """
        Write a comment section back to the file.

        Parameters
        ------
        comment: str
            A comment to write to the samconfg file
        """

        self.document = self.file_manager.put_comment(self.document, comment)

    def flush(self):
        """
        Write the data back to file
        """
        self._write()

    def sanity_check(self):
        """
        Sanity check the contents of samconfig
        """
        try:
            self._read()
        except SamConfigFileReadException:
            return False
        else:
            return True

    def exists(self):
        return self.filepath.exists()

    def _ensure_exists(self):
        self.filepath.parent.mkdir(parents=True, exist_ok=True)
        self.filepath.touch()

    def path(self):
        return str(self.filepath)

    @staticmethod
    def config_dir(template_file_path=None):
        """
        SAM Config file is always relative to the SAM Template. If it the template is not
        given, then it is relative to cwd()
        """
        if template_file_path:
            return os.path.dirname(template_file_path)

        return os.getcwd()

    def _read(self):
        if not self.document:
            try:
                self.document = self.file_manager.read(self.filepath)
            except FileParseException as e:
                raise SamConfigFileReadException(e) from e
        if self.document:
            self._version_sanity_check(self._version())
        return self.document

    def _write(self):
        if not self.document:
            return

        self._ensure_exists()

        current_version = self._version() if self._version() else SAM_CONFIG_VERSION
        self.document.update({VERSION_KEY: current_version})

        self.file_manager.write(self.document, self.filepath)

    def _version(self):
        return self.document.get(VERSION_KEY, None)

    def _deduplicate_global_parameters(self, cmd_name_key, section, key, env=DEFAULT_ENV):
        """
        In case the global parameters contains the same key-value pair with command parameters,
        we only keep the entry in global parameters

        Parameters
        ----------
        cmd_name_key : str
            key of command name

        section : str
            Specific section within the command to look into. e.g. `parameters`

        key : str
            Key to write the data under

        env : str
            Optional, Name of the environment
        """
        global_params = self.document.get(env, {}).get(DEFAULT_GLOBAL_CMDNAME, {}).get(section, {})
        command_params = self.document.get(env, {}).get(cmd_name_key, {}).get(section, {})
        if (
            cmd_name_key != DEFAULT_GLOBAL_CMDNAME
            and global_params
            and command_params
            and global_params.get(key)
            and global_params.get(key) == command_params.get(key)
        ):
            value = command_params.get(key)
            save_global_message = (
                f'\n\tParameter "{key}={value}" in [{env}.{cmd_name_key}.{section}] is defined as a global '
                f"parameter [{env}.{DEFAULT_GLOBAL_CMDNAME}.{section}].\n\tThis parameter will be only saved "
                f"under [{env}.{DEFAULT_GLOBAL_CMDNAME}.{section}] in {self.filepath}."
            )
            LOG.info(save_global_message)
            # Only keep the global parameter
            del self.document[env][cmd_name_key][section][key]

    @staticmethod
    def get_default_file(config_dir: str) -> str:
        """Return a defaultly-named config file, if it exists, otherwise the current default.

        Parameters
        ----------
        config_dir: str
            The name of the directory where the config file is/will be stored.

        Returns
        -------
        str
            The name of the config file found, if it exists. In the case that it does not exist, the default config
            file name is returned instead.
        """
        config_files_found = 0
        config_file = DEFAULT_CONFIG_FILE_NAME

        for extension in reversed(list(FILE_MANAGER_MAPPER.keys())):
            filename = DEFAULT_CONFIG_FILE + extension
            if Path(config_dir, filename).exists():
                config_files_found += 1
                config_file = filename

        if config_files_found == 0:  # Config file doesn't exist (yet!)
            LOG.debug("No config file found in this directory.")
        elif config_files_found > 1:  # Multiple config files; let user know which is used
            LOG.info(
                f"More than one samconfig file found; using {config_file}."
                f" To use another config file, please specify it using the '--config-file' flag."
            )

        return config_file

    @staticmethod
    def _version_sanity_check(version: Any) -> None:
        if not isinstance(version, float):
            raise SamConfigVersionException(f"'{VERSION_KEY}' key is not present or is in unrecognized format. ")

    @staticmethod
    def to_key(cmd_names: Iterable[str]) -> str:
        # construct a parsed name that is of the format: a_b_c_d
        return "_".join([cmd.replace("-", "_").replace(" ", "_") for cmd in cmd_names])


import sys
from collections import deque
input = sys.stdin.readline
sys.setrecursionlimit(5000)

n = int(input())

tree = [[] for _ in range(n+1)]
visited = [False] * (n+1)

for _ in range(n-1):
    a, b = map(int,input().split())
    tree[a].append(b)
    tree[b].append(a)

testCase = deque(map(int,input().split()))

def solve():
    cur = testCase.popleft()
    if not testCase:
        print(1)
        exit(0)
    visited[cur] = True
    for _ in range(len(tree[cur])):
        if testCase[0] in tree[cur] and not visited[testCase[0]]:
            solve()

    return False

if testCase[0] != 1:
    print(0)
    exit(0)
if not solve():
    print(0)

DFS 스페셜 저지.py


from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException
import random
from dataloader import DataLoader
import logging
from datetime import datetime
from mysql import MySQL

FORMAT = '%(asctime)s |%(levelname)s| %(message)s'
logging.basicConfig(level=logging.DEBUG, format=FORMAT, filename='./log/' + datetime.now().strftime('AFTS_%Y%m%d.log'))
retryCount = 0

def autoFill(id, cursor):
    options = Options()
    options.add_argument("--no-sandbox")
    options.add_argument("--headless")
    try:
        chrome = webdriver.Chrome(options=options)
        chrome.get("https://zh.surveymonkey.com/r/EmployeeHealthCheck")
        agreexpath = "//div[contains(@class,'radio-button-container')]//label//span[contains(@class,'radio-button-display')]"
        agreeCheck = WebDriverWait(chrome, 15).until(
            EC.visibility_of_element_located((By.XPATH, agreexpath)))
        # agreeCheck
        agreeCheck.click()

        # employId
        employeeId = chrome.find_elements_by_xpath(
            "//div[contains(@class,'question-body open-ended-single')]//input")[0]
        employeeId.send_keys(id)

        # foreheadTemp check
        foreheadCheckBtn = chrome.find_elements_by_xpath(
            "//div[contains(@class,'radio-button-container')]//label//span[contains(@class,'radio-button-display')]")[2]
        foreheadCheckBtn.click()

        # foreheadTemp
        Temperature = chrome.find_elements_by_xpath(
            "//div[contains(@class,'question-body open-ended-single')]//input")[1]
        foreheadDegree = str(round(random.uniform(35.1, 36.9), 1))
        Temperature.send_keys(foreheadDegree)

        # contacted people who returned from aboard in the last 14 days
        noContactBtn = chrome.find_elements_by_xpath(
            "//div[contains(@class,'radio-button-container')]//label//span[contains(@class,'radio-button-display')]")[5]
        noContactBtn.click()

        # declaration radio button
        declarationBtn = chrome.find_elements_by_xpath(
            "//div[contains(@class,'radio-button-container')]//label//span[contains(@class,'radio-button-display')]")[6]
        declarationBtn.click()

        # submit btn to next page
        submitBtn = chrome.find_elements_by_xpath(
            "//button[contains(text(), '下一頁')]")[0]
        submitBtn.click()

        # successful landing page
        compleredTxtPath = "(//span[@class='title-text'])"
        compleredTxt = WebDriverWait(chrome, 10, 1).until(
            EC.visibility_of_element_located((By.XPATH, compleredTxtPath))).text
        logging.info('Success: User ' + id + ' be filled. filled degree : ' + foreheadDegree)
        # print(id + " filled degree :" + foreheadDegree)
        sql = "UPDATE afts.OPERATION_HIST SET STATUS = 'DONE' WHERE (CONTEXT_ID = '{0}-{1}');".format(id, datetime.today().strftime("%y%m%d"))
        cursor.execute(sql)
        chrome.quit()
    except TimeoutException:
        chrome.quit()
        global retryCount
        retryCount += 1
        if(retryCount == 2):
            logging.error('Timeout:user ' + id + ' cannot be filled. Process end.')
            retryCount = 0
            pass
        else:
            logging.warning('Timeout:user ' + id + ' cannot be filled. Retrying...')
            autoFill(id, cursor)

# def readFile():
#     fileHandler = open("Id.txt", "r");
#     IdList = fileHandler.read().splitlines();
#     fileHandler.close();
#     return IdList;

if __name__ == "__main__":
    dt = DataLoader()
    users = dt.get_users()
    dt.update_load_status(users)
    print(users)
    db = MySQL()
    con = db.connect()
    with con:
        with con.cursor() as cursor:
            for user in users:
                autoFill(user, cursor)
        con.commit()


class Solution:
    def summaryRanges(self, nums):
        if not nums:
            return nums

        results = []
        start = end = nums[0]
        for i in nums:
            if i != end:
                rng = f"{start}->{end-1}" if start != (end - 1) else f"{start}"
                results.append(rng)
                start = end = i
            end += 1

        # we ended on a solo number.
        if start == nums[-1]:
            results.append(f'{start}')
        # we ended on a range.
        elif start != (end - 1):
            results.append(f'{start}->{end-1}')
        return results


# Çözüm - 1

default = 1  # Listeye 1 elemanını default olarak eklemek için
fibonacci_list = [default]
while fibonacci_list[-1] < 55: # Fibonacci listemizin son elemanı 55 den küçük olduğu sürece işleme devam eder
    fibonacci_list.append(sum(fibonacci_list[-2:])) # Listenin son iki elemanının toplamını her daim listeye eklemek için
print(fibonacci_list)

# Çözüm -2

number=1
a=0
liste = [number]
for i in range(number,56):
    if i == number:
        liste.append(number)
        a += 1
        number += liste[a-1]
print(liste)


# ----- standard modules -----
import os
import sys

import numpy as np
import scipy as sp
import pandas as pd         # pandas tabular DataFrame for task/behavioral data
import matplotlib as mpl    # plot
import matplotlib.pyplot as plt
import re                   # regular expression
import time                 # time code execution

# ----- modules of the project PyNeuroSG -----
import signal_align         # in this package: align neural data according to task
import PyNeuroAna as pna    # in this package: analysis
import PyNeuroPlot as pnp   # in this package: plot
import misc_tools           # in this package: misc

# ----- modules for the data location and organization in Sheinberg lab -----
import data_load_DLSH       # package specific for DLSH lab data
from GM32_layout import layout_GM32

""" -----  test using synthesit signal ----- """
x_grid = np.arange(0,16)
target = np.sin(2*np.pi/8*x_grid)
target_noisy = target* (1+np.random.randn(*target.shape)/3)
target_noisy[6] = 0
target_gauss = sp.ndimage.filters.gaussian_filter1d(target_noisy, 1)
reload(pna)
# target_smooth = pna.quad_smooth_d3(target=target_noisy, lambda_der=0.5)
target_smooth, csd_smooth = pna.quad_smooth_der(target=target_noisy, lambda_der=1, degree_der=3, return_CSD=True)

_, h_axes = plt.subplots(3,2)
plt.axes(h_axes[0,0])
plt.title('LFP')
plt.plot(x_grid, target)
plt.plot(x_grid, target_noisy)
plt.axes(h_axes[1,0])
plt.plot(x_grid, target)
plt.plot(x_grid, target_gauss)
plt.axes(h_axes[2,0])
plt.plot(x_grid, target)
plt.plot(x_grid, target_smooth)

# plt.legend(['origianl', 'noisy','gaussian_smooth' , 'smoothed'])

csd = pna.cal_1dCSD(target, tf_edge=True)
csd_noisy = pna.cal_1dCSD(target_noisy, tf_edge=True)
csd_gauss = pna.cal_1dCSD(target_gauss, tf_edge=True)
# csd_gauss_after = sp.ndimage.filters.gaussian_filter1d(csd_noisy, 1)
# csd_smooth = pna.cal_1dCSD(target_smooth, tf_edge=True)

plt.axes(h_axes[0,1])
plt.title('CSD')
plt.plot(x_grid, csd, '-', linewidth=2 )
plt.plot(x_grid, csd_noisy, '-', linewidth=2 )
plt.legend(['origianl', 'native estimator'])
plt.axes(h_axes[1,1])
plt.plot(x_grid, csd, '-', linewidth=2 )
plt.plot(x_grid, csd_gauss, '-', linewidth=2 )
# plt.plot(x_grid, csd_gauss_after, '--', linewidth=2 )
plt.legend(['origianl', 'gaussian smoothed'])
plt.axes(h_axes[2,1])
plt.plot(x_grid, csd, '-', linewidth=2 )
plt.plot(x_grid, csd_smooth, '-', linewidth=2 )
plt.legend(['origianl', 'derevative smoothed'])

dir_tdt_tank = '/shared/homes/sguan/neuro_data/tdt_tank'
dir_dg='/shared/homes/sguan/neuro_data/stim_dg'

def GetERP(tankname='GM32.*U16.*161125'):
    [blk, data_df, name_tdt_blocks] = data_load_DLSH.load_data('d_.*srv_mask.*', tankname, tf_interactive=False,
                                                               dir_tdt_tank=dir_tdt_tank, dir_dg=dir_dg)

    """ Get StimOn time stamps in neo time frame """
    ts_StimOn = data_load_DLSH.get_ts_align(blk, data_df, dg_tos_align='stimon')

    """ some settings for saving figures  """
    filename_common = misc_tools.str_common(name_tdt_blocks)
    dir_temp_fig = './temp_figs'

    """ make sure data field exists """
    data_df = data_load_DLSH.standardize_data_df(data_df, filename_common)
    blk     = data_load_DLSH.standardize_blk(blk)

    t_plot = [-0.100, 0.500]

    data_neuro=signal_align.blk_align_to_evt(blk, ts_StimOn, t_plot,
                                             type_filter='ana.*', name_filter='LFPs.*', chan_filter=range(1,48+1))
    ERP = np.mean(data_neuro['data'], axis=0).transpose()

    return ERP, data_neuro['ts']

ERP, ts = GetERP(tankname='GM32.*U16.*161206')

pnp.ErpPlot(ERP[32:, :], ts)

lfp = ERP[32:, :]

reload(pna); csd = pna.cal_1dCSD(lfp, axis_ch=0); pnp.ErpPlot(pna.SmoothTrace(csd, sk_std=2.0, fs=1.0, axis=0), ts)

""" test quadratic smoothing """
lfp = ERP[32:48, :]
# lfp *= np.expand_dims(np.arange(len(lfp))!=8, axis=1)
reload(pna)
# lfp = lfp/np.std(lfp, axis=1, keepdims=True)
lambda_dev = np.ones(16)
lambda_dev[2] = 0
lfp_sm_gau = pna.lfp_cross_chan_smooth(lfp, method='gaussian', sigma_chan=1.5, sigma_t=0.5)
lfp_sm_der = pna.lfp_cross_chan_smooth(lfp, method='der', lambda_dev=lambda_dev, lambda_der=1, sigma_t=0.5)

csd_nai        = pna.cal_1dCSD(lfp, axis_ch=0, tf_edge=True)
csd_gau_before = pna.cal_1dCSD(lfp_sm_gau, axis_ch=0, tf_edge=True)
csd_gau_after  = pna.lfp_cross_chan_smooth(csd_nai, method='gaussian', sigma_chan=0.5, sigma_t=0)
csd_der        = pna.cal_1dCSD(lfp_sm_der, axis_ch=0, tf_edge=True)

pnp.ErpPlot(lfp, ts); plt.suptitle('original lfp')

pnp.ErpPlot(csd_nai,        ts, title='CSD naive'); plt.suptitle('csd from original lfp')
pnp.ErpPlot(csd_gau_before, ts, title='CSD'); plt.suptitle('CSD, gaussian smoothed on lfp')
pnp.ErpPlot(csd_gau_after,  ts, title='CSD'); plt.suptitle('CSD, gaussian smoothed on csd')
pnp.ErpPlot(csd_der,        ts, title='CSD'); plt.suptitle('CSD, derivative smoothed')


n = int(input())

i=0
d = []
while i<n:
	s,f = (int(x) for x in input().split())
	d.append([f,s])
	i+=1

dS = sorted(d)
sum = 0
fail = False
for x in dS:
	sum+=x[1]
	if sum>x[0]:
		fail = True
		break

if fail:
	print("No")
else:
	print("Yes")

Progvar/AP CSCI 21 2022/Week 4/Megalomania.py


import os
import xml.etree.ElementTree
from bs4 import BeautifulSoup
import cv2
import random
import shutil
import time
import numpy as np

start = time.time()

target_path = 'D:\신지영_업무\\0709_어트리뷰트인수인계\\attribute_crop_images\\upper\origin'
test_set_path = 'D:\신지영_업무\\0709_어트리뷰트인수인계\\attribute_crop_images\\upper\Test_Set'
file_path = 'D:\신지영_업무\\0709_어트리뷰트인수인계\\attribute_crop_images\\upper\\file_list.txt'

crop_img_list = []
xml_list = []
file_name_list = []
attri_list = []
shortshirt_list = []
longshirt_list = []
shirtdk_list = []
type_name_list = ['shortshirt', 'longshirt', 'shirtdk']
color_name_list = ['red', 'green', 'yellow', 'blue', 'brown', 'pink', 'gray', 'black', 'white','unknown']

attri_target_count = 0
shortshirt = 0
longshirt = 0
shirtdk = 0
top_red = 0
top_green = 0
top_yellow = 0
top_blue = 0
top_brown = 0
top_pink = 0
top_gray = 0
top_black = 0
top_white = 0
top_color_unknown = 0
xml_count = 0

red_list = []
green_list = []
yellow_list = []
blue_list = []
brown_list = []
pink_list = []
grey_list = []
black_list = []
white_list = []
unknown_list = []

target_file_list = os.listdir(target_path)
for x in target_file_list:
    if x.endswith('png'):
        crop_img_list.append(x)
    elif x.endswith('xml'):
        xml_list.append(x)
        temp = x.split('.')
        file_name_list.append(temp[0])

def split_label(label):
    temp = str(label)
    temp0 = temp.split('<')
    temp1, result = temp0[1].split('>')
    return result

def split_name(name):
    temp = str(name)
    try:
        temp0 = temp.split("\"")
        result = temp0[1]
    except:
        return False
    return result

def comparison(value):
    sort_list = value.copy()
    empty_list = value.copy()
    sort_list.sort()
    return_count = 0
    count = 0
    count_list = []
    result = []
    num_len = len(value)
    if num_len <= 3:
        for x in value:
            if sort_list[0] == x:
                return count
            else:
                count += 1
    else:
        for x in sort_list:
            count = 0
            for y in empty_list:
                if y == x:
                    result.append(str(count))
                    return_count +=1
                    empty_list.pop(count)
                    break
                else:
                    count += 1
            if return_count == 3:
                break
    return result

file_attri = []
print("xml을 읽어 속성 값 count 시작")

for xml in xml_list:
    if xml.endswith('xml'):
        xml_path = os.path.join(target_path, xml)
        file = open(xml_path, 'r', encoding='utf-8')
        lines = ''
        while True:
            line = file.readline()
            if not line: break
            lines += line
        file.close()
        bs = BeautifulSoup(lines, 'lxml')
        str_temp = '%s' % file_name_list[xml_count]
        temp = []
        temp_str = ''
        for box in bs.find_all('box'):
            label = 'upper'
            lines = str(box).splitlines()
            for x in lines[1:]:
                result = split_label(x)
                if result != 'false':
                    label_name = split_name(x)
                    if label_name:
                        if label_name == 'top':
                            if result == 'short_sleeve':
                                shortshirt += 1
                                shortshirt_list.append(file_name_list[xml_count])
                                temp_str += 'short_sleeve,'
                            elif result == 'long_sleeve':
                                longshirt += 1
                                longshirt_list.append(file_name_list[xml_count])
                                temp_str += 'long_sleeve,'
                            else:
                                shirtdk += 1
                                shortshirt_list.append(file_name_list[xml_count])
                                temp_str += 'shirtdk,'
                            attri = result
                        else:
                            if label_name == 'top_red':
                                top_red += 1
                                temp_str += 'top_red'
                                red_list.append(file_name_list[xml_count])
                            elif label_name == 'top_yellow':
                                top_yellow += 1
                                yellow_list.append(file_name_list[xml_count])
                                temp_str += 'top_yellow'
                            elif label_name == 'top_green':
                                top_green += 1
                                green_list.append(file_name_list[xml_count])
                                temp_str += 'top_green'
                            elif label_name == 'top_blue':
                                top_blue += 1
                                blue_list.append(file_name_list[xml_count])
                                temp_str += 'top_blue'
                            elif label_name == 'top_black':
                                top_black += 1
                                black_list.append(file_name_list[xml_count])
                                temp_str += 'top_black'
                            elif label_name == 'top_brown':
                                top_brown += 1
                                brown_list.append(file_name_list[xml_count])
                                temp_str += 'top_brown'
                            elif label_name == 'top_pink':
                                top_pink += 1
                                pink_list.append(file_name_list[xml_count])
                                temp_str += 'top_pink'
                            elif label_name == 'top_grey':
                                top_grey += 1
                                grey_list.append(file_name_list[xml_count])
                                temp_str += 'top_grey'
                            elif label_name == 'top_white':
                                top_white += 1
                                white_list.append(file_name_list[xml_count])
                                temp_str += 'top_white'
                            elif label_name == 'top_color_unknown':
                                top_color_unknown += 1
                                unknown_list.append(file_name_list[xml_count])
                                temp_str += 'top_color_unknown'
                            attri = label_name
                    temp = temp_str
                    if not (str(attri) in str_temp):
                        str_temp += ',%s' % str(attri)
    file_attri.append(temp)
    attri_list.append(str_temp)
    xml_count += 1

total_list = [longshirt, shortshirt, shirtdk, top_red, top_brown,
              top_color_unknown, top_white, top_black, top_grey,
              top_blue, top_green, top_yellow, top_pink]
total_name_list = ['longshirt', 'shortshirt', 'shirtdk', 'top_red', 'top_brown',
              'top_color_unknown', 'top_white', 'top_black', 'top_grey',
              'top_blue', 'top_green', 'top_yellow', 'top_pink']
min_name = comparison(total_list)
temp = len(min_name)
maximum = total_list[int(temp)-1]
attri_min_name_list = []
attri_min_list = []
pop_file_name_list = []

for x in min_name:
    temp = int(x)
    attri_min_list.append(total_list.pop(temp))
    attri_min_name_list.append(total_name_list.pop(temp))
min_count = 0
result_list = []
unknown_copy_list = unknown_list.copy()
shirtdk_copy_list = shirtdk_list.copy()
brown_copy_list = brown_list.copy()
print("테스트셋에 들어갈 파일 분류 시작")

while True: #분류진행 코드
    if min_count == 0:
        if top_color_unknown >= 1:
            temp = len(unknown_copy_list)
            list_len = int(temp)
            if list_len > 1:
                random_num = random.randint(0, list_len-1)
                target_file = unknown_copy_list.pop(random_num)
                top_color_unknown -= 1
            else:
                target_file = unknown_copy_list[0]
            if not(target_file in result_list):
                result_list.append(target_file)
        else:
            print("첫 번째 최소값 분류 완료")
            min_count += 1
    elif min_count == 1: #shirtdk
        if shirtdk >= 1:
            temp = len(shirtdk_copy_list)
            list_len = int(temp)
            if list_len > 1:
                random_num = random.randint(0, list_len - 1)
                target_file = shirtdk_copy_list.pop(random_num)
                shirtdk -= 1
            else:
                target_file = shirtdk_copy_list[0]
            if not(target_file in result_list):
                result_list.append(target_file)
        else:
            print("두 번째 최소값 분류 완료")
            min_count += 1
    elif min_count == 2: #top_brown
        if top_brown >= 1:
            temp = len(brown_copy_list)
            list_len = int(temp)
            if list_len > 1:
                random_num = random.randint(0, list_len-1)
                target_file = brown_copy_list.pop(random_num)
                top_brown -= 1
            else:
                target_file = brown_copy_list[0]
            result_list.append(target_file)
            if not (target_file in result_list):
                result_list.append(target_file)
        else:
            print("세 번째 최소값 분류 완료")
            break
temp = ''
for x in result_list:
    temp += x+'\n'
file = open(file_path, 'w')
file.write(temp)
file.close()

print("EXIT")
print(time.time()-start)


import numpy as np

from news_classifier.constants import LABEL_COL, UNK_TOKEN, SOS_TOKEN, EOS_TOKEN, TITLE_COL
from news_classifier.preprocessing import clean_headline, tokenize_headlines

class Vocabulary:

    def __init__(
        self,
        token_to_index: dict=None,
        tokens: list=None,
        unk_token: bool=True,
        sos_token: bool=True,
        eos_token: bool=True
        ):
        """
        """
        self._token_to_index = {} if token_to_index is None else token_to_index
        self._tokens = [] if tokens is None else tokens
        self.unk_token = unk_token
        self.sos_token = sos_token
        self.eos_token = eos_token

        if unk_token:
            self.add_token(UNK_TOKEN)
        if sos_token:
            self.add_token(SOS_TOKEN)
        if eos_token:
            self.add_token(EOS_TOKEN)

    def add_token(self, token):
        if token in self._token_to_index:
            return
        index = len(self._tokens)
        self._token_to_index[token] = index
        self._tokens.append(token)

    @classmethod
    def from_serializable(cls, dictionary):
        return cls(**dictionary)

    def to_serializable(self):
        return {
            'token_to_index': self._token_to_index,
            'tokens': self._tokens,
            'unk_token': self.unk_token,
            'sos_token': self.sos_token,
            'eos_token': self.eos_token
        }

    def lookup_token(self, token):
        if token in self._token_to_index:
            return self._token_to_index[token]
        if self.unk_token:
            return self._token_to_index[UNK_TOKEN]
        raise KeyError(f'Token {token} does not belong to this vocab and uknown tokens are turned off')

    def lookup_index(self, index):
        if index >= len(self._tokens):
            raise KeyError(f'Index {index} does not belong to this vocab')
        return self._tokens[index]

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

class Vectorizer:

    def __init__(self, headlines_vocab, labels_vocab):
        self.headlines_vocab = headlines_vocab
        self.labels_vocab = labels_vocab

    def vectorize_headline(self, headline):
        cleaned_headline = clean_headline(headline)
        tokens = tokenize_headlines(cleaned_headline)

        one_hot = np.zeros(len(self.headlines_vocab), dtype=np.float32)
        for token in tokens:
            index = self.headlines_vocab.lookup_token(token)
            one_hot[index]  = 1
        return one_hot

    def vectorize_category(self, category):
        return self.labels_vocab.lookup_token(category)

    @classmethod
    def from_dataframe(cls, df):
        headlines_vocab = Vocabulary()
        labels_vocab = Vocabulary(sos_token=False, eos_token=False, unk_token=False)
        for _, row in df.iterrows():
            headline, label = row[TITLE_COL], row[LABEL_COL]
            cleaned_headline = clean_headline(headline)
            tokens = tokenize_headlines(cleaned_headline)
            for token in tokens:
                headlines_vocab.add_token(token)
            labels_vocab.add_token(label)
        return cls(headlines_vocab, labels_vocab)

    @classmethod
    def from_serializable(cls, contents):
        headlines_vocab = Vocabulary.from_serializable(contents['headlines_vocab'])
        labels_vocab = Vocabulary.from_serializable(contents['labels_vocab'])
        return cls(headlines_vocab, labels_vocab)

    def to_serializable(self):
        return {
            'headlines_vocab': self.headlines_vocab.to_serializable(),
            'labels_vocab': self.labels_vocab.to_serializable()
               }


"""
Asserts the results of an execution of the Security Scorecards tool.
"""
import base64
import hashlib
import json
import logging
import os
import typing
from collections import defaultdict

from ..evidence import RedactedEvidence, Reproducibility, FileEvidence
from ..sarif import SarifHelper
from ..subject import BaseSubject
from .base import BaseAssertion
from ..utils import strtobool

class SecurityToolFinding(BaseAssertion):
    """
    Asserts the aggregate results of the execution of a tool against a subject.

    :param subject: The subject to assert against.
    :param input_file: The input file to use instead of running the tool (optional).

    Logic:
    - If the file type isn't SARIF, exit.
    - Aggregate the number of findings based on severity.
    - Severity is defined as:
      - TBD
    """

    def __init__(self, subject: BaseSubject, **kwargs):
        super().__init__(subject, **kwargs)

        self.data = None  # type: typing.Optional[dict]
        self.filtered_results = []  # type: typing.Generator[dict, None, None]
        self.severity_map = {}  # type: dict[str, int]
        self.include_evidence = strtobool(kwargs.get("include_evidence", True))
        self.filter = kwargs.get("filter", None)
        if self.filter:
            if self.filter.startswith('lambda '):
                self.filter = eval(self.filter)     # Dangerous, need to be careful here.

        self.input_file = kwargs.get("input_file")
        if not self.input_file or not os.path.isfile(self.input_file):
            raise IOError(f"Input file [{self.input_file}] does not exist")

        self.set_generator("security_tool_finding", "0.1.0", True)

    def process(self):
        """Process the assertion."""
        if not os.path.exists(self.input_file):
            raise ValueError("Input file does not exist.")

        logging.debug("Reading input file: %s", self.input_file)

        with open(self.input_file, "r", encoding="utf-8") as f:
            _content = f.read()
            logging.debug("Read %d bytes from input file.", len(_content))

            self.data = json.loads(_content)
            if self.include_evidence:
                self.evidence = FileEvidence(self.input_file, _content, Reproducibility.UNKNOWN)
            else:
                self.evidence = RedactedEvidence(
                    {
                        "digest": base64.b64encode(
                            hashlib.sha256(_content.encode("utf-8")).digest()
                        ).decode("ascii"),
                        "alg": "sha256",
                    },
                    Reproducibility.UNKNOWN,
                )

        sarif_helper = SarifHelper(self.data)
        self.filtered_results = sarif_helper.filter(self.filter)

        self.severity_map = defaultdict(int)

        for result in self.filtered_results:
            self.severity_map[self._get_severity(result)] += 1

        logging.debug("Aggregate results: %s", dict(self.severity_map))

    @staticmethod
    def _get_severity(result) -> str:
        """Returns the severity of the result."""
        return result.get("rule_severity", "unknown")

    def emit(self) -> None:
        """Emits a security advisory assertion for the targeted package."""
        self.assertion["predicate"].update(
            {
                "content": {"severity": self.severity_map},
                "evidence": self.evidence.to_dict() if self.evidence else None,
            }
        )


from django.shortcuts import render, redirect
from django.http import HttpResponse
from .models import Search
from .forms import SearchForm
import folium
import geocoder

# Create your views here.

def index(request):
    if request.method == "POST":
        form = SearchForm(request.POST)
        if form.is_valid():
            form.save()
            return redirect('/')

    else:
        form = SearchForm()
    address = Search.objects.all().last()
    # address = request.POST.get('address')
    location = geocoder.osm(address)
    print(location.osm)

    lat = location.lat
    lng = location.lng
    country = location.country
    print(f" Longitude: {lng} - Latitude: {lat} <--> {country}")

    if (lat == None) or (lng == None):
        address.delete()
        return HttpResponse('Address is not valid!')

    # Create map object
    mapObj = folium.Map(location=[44.0, 50.0], zoom_start=2)

    folium.Marker([lat, lng], tooltip="Click for more", popup=country).add_to(mapObj)

    # Get HTML Representation of Map Object
    mapObj = mapObj._repr_html_()
    context = {
        'mapObj': mapObj,
        'form': form,
    }
    return render(request, 'index.html', context)


import random

import discord
from discord.ext import commands

import cogs.mods.itunes as itunes
import cogs.mods.base as base

class ArtistSearch:

    def __init__(self, bot):
        self.bot = bot

    @commands.command(name='search_artist', aliases=['creator', 'artist', 'person', 'musician', 'people', 'search_person', 'search_musician', 'search_creator', 'searchartist', 'artist_search'])
    @commands.cooldown(10, 4, commands.BucketType.guild)
    @commands.cooldown(1, 3, commands.BucketType.user)
    async def search_artist(self, ctx, *, artist_name=None):

        # Sometimes doesnt work??? IDK

        if not artist_name:
            cmdprefix = (await self.bot.command_prefix(self.bot, ctx.message))[-1]
            embed = discord.Embed(
                color=random.randint(0x000000, 0xffffff),
                title=f'Help for `{cmdprefix}{ctx.invoked_with}`',
                description=f'`{cmdprefix}{ctx.invoked_with} <artist name>`'
            )
            embed.set_image(
                url='https://i.gyazo.com/7d33394b56834ca32dc4c5db068806fe.png'
            )
            embed.set_footer(
                text="Trackrr Music Search",
                icon_url="https://media.discordapp.net/attachments/452763485743349761/452763575878942720/TrackrrLogo.png"
            )
            return await ctx.send(embed=embed)

        async with ctx.channel.typing():
            try:
                embed = await itunes.search_artist(artist_name)
            except base.NotFound:
                embed = discord.Embed(
                    title=f'Trackrr',
                    description=f'No results found for `{artist_name}`!'
                )
            embed.set_footer(
                text="Trackrr Music Search | Data pulled from Apple Music",
                icon_url="https://media.discordapp.net/attachments/452763485743349761/452763575878942720/TrackrrLogo.png"
            )
            await ctx.send(embed=embed)

def setup(bot):
    bot.add_cog(ArtistSearch(bot))


import torch
import torch.nn as nn
import torch.nn.functional as F

class transformer(nn.Module):

    def __init__(self, in_chans=256, k=4, dc=28):
        super(transformer, self).__init__()
        r = 2*k+1
        self.conv1 = nn.Conv2d(in_chans*2, 256, 3, 1, 1, bias=True)
        self.conv2 = nn.Conv2d(256, 192, 3, 1, 1, bias=True)
        self.conv3 = nn.Conv2d(192, 128, 3, 1, 1, bias=True)
        self.conv4 = nn.Conv2d(128, 96, 3, 1, 1, bias=True)
        self.conv5 = nn.Conv2d(96, 64, 3, 1, 1, bias=True)
        self.conv6 = nn.Conv2d(64, dc, 3, 1, 1, bias=True)
        self.pool = nn.AdaptiveAvgPool2d(r)

        self.relu = nn.ReLU(inplace=True)

    def forward(self, x):
        b = x.shape[0]
        x = self.relu(self.conv1(x))
        x = self.relu(self.conv2(x))
        x = self.relu(self.conv3(x))
        x = self.relu(self.conv4(x))
        x = self.relu(self.conv5(x))
        x = self.conv6(x)
        x = self.pool(x)

        return x.view(b, 1, 1, 2, -1)

def upflow8(flow, mode='bilinear'):
    new_size = (8 * flow.shape[2], 8 * flow.shape[3])
    return  8 * F.interpolate(flow, size=new_size, mode=mode, align_corners=True)

if __name__ == '__main__':
    pass


"""
-*- coding: utf-8 -*-

File: OCTantApp.py

Class OCTantApp

.. inheritance-diagram:: OCTantApp

Application entrance point.

:Log:

+-------------+--------+------------------------------------------------------+
| Date        | Author | Description                                          |
+=============+========+======================================================+
| Feb-2018    | AACA   | - File created as run.py                             |
+-------------+--------+------------------------------------------------------+
| 4-Aug-2018  | FOE    | - Isolated minimal solution.                         |
+-------------+--------+------------------------------------------------------+
| 21-Aug-2018 | FOE    | - Updated main window class from OCTToolsMainWindow  |
|             |        |   to IOT_GUI_MainWindow (later rebranded as          |
|             |        |   IOT_GUI_ToolsWindow                                |
+-------------+--------+------------------------------------------------------+
| 22-Aug-2018 | FOE    | - Run is now a class. File rebranded to OCTToolsApp  |
|             |        |   from run.py                                        |
|             |        | - The application main window is no longer the tools |
|             |        |   menu window (IOT_GUI_ToolsWindow), but the document|
|             |        |   window (IOT_GUI_DocumentWindow).                   |
+-------------+--------+------------------------------------------------------+
| 9-Sep-2018  | FOE    | - "Separated" the entry points as a script, from the |
|             |        |   entry point as an installed program.               |
|             |        | - Added "src" to the path for "installed" execution. |
+-------------+--------+------------------------------------------------------+
| 23-Sep-2018 | FOE    | - Updated comments and added Sphinx                  |
|             |        |   documentation to the class                         |
+-------------+--------+------------------------------------------------------+
| 14-Nov-2018 | FOE    | - Connected the tools window to the document window. |
+-------------+--------+------------------------------------------------------+
| 13-Feb-2019 | FOE    | - Updated support for version control. See version.py|
+-------------+--------+------------------------------------------------------+
| 28-Feb-2019 | FOE    | - Rebranded as OCTantApp.py                          |
|             |        | - Adapted to new package OCTant structure.           |
|             |        | - Importing statements for classes within the        |
|             |        |   OCTant package have been updated.                  |
+-------------+--------+------------------------------------------------------+
|  3-Mar-2019 | FOE    | - Tools window is now a child dock of DocumentWindow |
|             |        |   which is left as the only QMainWindow of the app.  |
+-------------+--------+------------------------------------------------------+
| 17-Mar-2019 | FOE    | - Added properties appsettings and appsettingsfile.  |
|             |        | - docWindow attribute converted to docwindow         |
|             |        |   property.                                          |
|             |        | - Cleaner exit with call to deleteLater              |
|             |        | - Removed method show. Now the document window show  |
|             |        |   is called accesing the docwindow property.         |
|             |        | - Class OCTantApp now becomes a QApplication         |
|             |        |   (previously we had 2 separated objects; one for the|
|             |        |   QApplication and another just for "holding" the    |
|             |        |   the main window.)                                  |
+-------------+--------+------------------------------------------------------+
| 31-Mar-2019 | FOE    | - Fixed comments of property docwindow which were    |
|             |        |   referring to Settings.                             |
+-------------+--------+------------------------------------------------------+

.. seealso:: None
.. note:: None
.. todo:: None

.. sectionauthor:: Felipe Orihuela-Espina <f.orihuela-espina@inaoep.mx>
.. codeauthor:: Arlem Aleida Castillo Avila <acastillo@inaoep.mx>
.. codeauthor:: Felipe Orihuela-Espina <f.orihuela-espina@inaoep.mx>

"""

## Import
import sys
#Add paths
if not sys.path[0] == '..':
    sys.path.insert(0, '..')
#sys.path.append('..')
                    #This line is not needed while
                    #working in the interpreter, as the main file is in the
                    #same directory. However, it is needed by pynsist when
                    #it creates the installer, so that it "sees" the src
                    #folder.

from PyQt5.QtWidgets import QApplication
#, QMainWindow, QWidget, QHBoxLayout, QTabWidget, QPushButton, QGraphicsView, QLabel, QAction, QFileDialog
#from PyQt5.QtGui import QIcon, QPixmap
#from PyQt5 import uic

from version import __version__
from DocumentWindow import DocumentWindow
#from ToolsDock import ToolsDock

import octant as oc

## Class definition
class OCTantApp(QApplication):
    #Sphinx documentation
    """Application entrance point.

    Application entrance point.

    .. seealso:: None
    .. note:: None
    .. todo:: None

    """

        #Private class attributes shared by all instances

    #Class constructor
    #def __init__(self, **kwargs):
    def __init__(self, kwargs): #DO NOT use the classical ** before kwargs in this case!
        print("OCTant App v" + __version__ + ": Initiating...")
        #Call superclass constructor
        super().__init__(kwargs) #For Python 3

        #Initialize private attributes unique to this instance
        self.document = None #The current document. Initialize a document
        self.docwindow = DocumentWindow(theparentapp = self)
        #self.docwindow.parentapp = self #Connect the window with the application

        self.appsettingsfile = '..\\..\\resources\\OCTantApp.config'
        self.appsettings = oc.data.Settings()

        print("Reading application settings from " +  self.appsettingsfile + ".")
        self.appsettings.read(self.appsettingsfile)
        #print(tmp.appsettings)

        #Finally, show the main window
        self.docwindow.show()

        return

#    def show(self):
#        #self.toolsWindow.show()
#        self.docwindow.show()

    #Properties getters/setters
    #
    # Remember: Sphinx ignores docstrings on property setters so all
    #documentation for a property must be on the @property method
    @property
    def document(self): #document getter
        """
        The application opened document.

        :getter: Gets the application opened document
        :setter: Sets the application opened document
        :type: class:`octant.data.Document`
        """
        return self.__document

    @document.setter
    def document(self,newDoc): #document setter
        if newDoc is None:
            newDoc = oc.data.Document() #Initialize a document
        if (type(newDoc) is oc.data.Document):
            self.__document = newDoc
        else:
            warnMsg = self.getClassName() + ':document: Unexpected document type.'
            warnings.warn(warnMsg,SyntaxWarning)
        return None

    @property
    def docwindow(self): #appsettings getter
        """
        Access the application main window

        :getter: Gets the application main window
        :setter: Sets the application main window
        :type: class:`octant.app.DocumentWindow`
        """
        return self.__docwindow

    @docwindow.setter
    def docwindow(self,newDocWindow): #document setter
        if newDocWindow is None:
            newDocWindow = DocumentWindow() #Initialize settings
        if (type(newDocWindow) is DocumentWindow):
            self.__docwindow = newDocWindow
        else:
            warnMsg = self.getClassName() + ':docwindow: Unexpected docwindow type.'
            warnings.warn(warnMsg,SyntaxWarning)
        return None

    @property
    def appsettings(self): #appsettings getter
        """
        The application settings.

        :getter: Gets the application settings
        :setter: Sets the application settings
        :type: class:`octant.data.Settings`
        """
        return self.__appsettings

    @appsettings.setter
    def appsettings(self,newSettings): #document setter
        if newSettings is None:
            newSettings = oc.data.Settings() #Initialize settings
        if (type(newSettings) is oc.data.Settings):
            self.__appsettings = newSettings
        else:
            warnMsg = self.getClassName() + ':appsettings: Unexpected settings type.'
            warnings.warn(warnMsg,SyntaxWarning)
        return None

    @property
    def appsettingsfile(self): #appsettings getter
        """
        The settings file name.

        :getter: Gets the current settings file name.
        :setter: Sets the current settings file name.
        :type: str
        """
        return self.__appsettingsfile

    @appsettingsfile.setter
    def appsettingsfile(self,newFilename): #document setter
        if newFilename is None:
            newFilename = '..\\..\\resources\\OCTantApp.config' #Initialize
        if (type(newFilename) is str):
            self.__appsettingsfile = newFilename
        else:
            warnMsg = self.getClassName() + ':appsettingsfile: Unexpected settings filename type.'
            warnings.warn(warnMsg,SyntaxWarning)
        return None

#This is the application entrance point. Note that it is NOT a method of the OCTantApp class
def main():

    from OCTantApp import OCTantApp #Make the import explicit so that class type is OCTantApp instead of __main__.OCTantApp

    #Instancia para iniciar una aplicación
    myApp = OCTantApp(sys.argv) #The application object. OCTantApp is a QApplication
    #myApp = OCTantApp() #OCTantApp is a QApplication
    myApp.aboutToQuit.connect(myApp.deleteLater) #Ensures the application is
                                    #deleted after closing, and in practice
                                    #removes a closing exception.
    #Starts the event loop and exit upon quitting
    #QApplication quits when the last primary window (window with no parent) is closed
    myApp.exec_()
    sys.exit(0)

# The following two lines, allow me to run the program from the interpreter
#"as a script", while the above method main() allow me to have an entrance
#point so that it is possible to call the method from an installed version
#of the application
if __name__ == "__main__":
    main()


import datetime
import json
from flask_caching import Cache
import concurrent.futures
from flask import Flask, session, url_for, request, render_template, redirect, g
import os
import dbContext
import utils
import pickle

app = Flask(__name__, template_folder='templates')
app.secret_key = os.urandom(24)
cache = Cache(app, config={'CACHE_TYPE': 'simple'})

# Load the trained ML pickle file
monday = pickle.load(open('./static/pickle_files/monday_station.pkl', 'rb'))
tuesday = pickle.load(open("./static/pickle_files/tuesday_station.pkl", "rb"))
wednesday = pickle.load(
    open("./static/pickle_files/wednesday_station.pkl", "rb"))
thursday = pickle.load(
    open("./static/pickle_files/thursday_station.pkl", "rb"))
friday = pickle.load(open("./static/pickle_files/friday_station.pkl", "rb"))
saturday = pickle.load(
    open("./static/pickle_files/saturday_station.pkl", "rb"))
sunday = pickle.load(open("./static/pickle_files/sunday_station.pkl", "rb"))

# router to the prediction service
@app.route("/prediction", methods=['GET', 'POST'])
def prediction_model():
    import numpy as np

    # Store the request from JS
    data = request.args.get('post', 0, type=str)
    print(data)
    data = data.split()
    temperature = float(data[0])
    pressure = int(data[1])
    humidity = int(data[2])
    wind_speed = float(data[3])
    date = (data[4])
    d = datetime.datetime.strptime(date, "%Y-%m-%d")
    date = d.strftime("%A")
    minute = (data[5])
    station = int(data[6])
    d = datetime.datetime.strptime(minute, "%H:%M")
    hours = int(d.hour)
    minute = int(d.minute)

    print("Data to be sent to the prediction model ", data)
    print(type(data))
    prediction_input = [[station, temperature,
                         pressure, humidity, wind_speed, hours, minute]]
    if date == "Monday":
        x = monday.predict(prediction_input)
    elif date == "Tuesday":
        x = tuesday.predict(prediction_input)
    elif date == "Wednesday":
        x = wednesday.predict(prediction_input)
    elif date == "Thursday":
        x = thursday.predict(prediction_input)
    elif date == "Friday":
        x = friday.predict(prediction_input)
    elif date == "Saturday":
        x = saturday.predict(prediction_input)
    elif date == "Sunday":
        x = sunday.predict(prediction_input)

    print("Predicted available bikes for selected station is", int(x[0]))

    # Fetch the ML model output and return as JSON to client
    prediction = [int(x[0])]
    return json.dumps(prediction)

@app.route('/', methods=['GET', 'POST'])
def index():
    if request.method == 'POST':
        session.pop('user', None)
        if dbContext.login(request.form['email'], request.form['password']):
            session['user'] = request.form['email']
            return redirect(url_for('dashboard'))
        else:
            return render_template('index.html', results=False)
    if session.get('from_register_page'):
        return render_template('index.html', from_register_page=True)
    return render_template('index.html')

@app.route('/register', methods=['GET', 'POST'])
def register():
    if request.method == 'POST':
        validation_results = utils.validate_register_information(request.form['firstName'], request.form['lastName'],
                                                                 request.form['email'], request.form['password'],
                                                                 request.form['confirmPassword'])
        if not validation_results.results:
            return render_template('register.html', errors=validation_results.data)

        results = dbContext.register(request.form['firstName'], request.form['lastName'], request.form['email'],
                                     request.form['password'])
        if results[0]:
            session['from_register_page'] = True
            return redirect(url_for('index', from_register_page=True))
        else:
            return render_template('register.html', errors=results[1])
    return render_template('register.html')

@app.route('/searchStation', methods=['GET'])
def search_station():
    try:
        # return "Pew Pew", 404
        data = dbContext.get_station(name=request.args.get('name'))
        if len(data) < 1:
            return "Station not found.", 404
        return json.dumps(data[0])

    except Exception as e:
        return f"Something went wrong. Please try again. {e}", 500

@app.route('/dashboard')
@cache.cached(timeout=300)  # Cache the page for 5 minutes
def dashboard():
    if g.user:
        stations = None
        bikes = None
        weather = None
        current_availability = None
        with concurrent.futures.ThreadPoolExecutor() as executor:
            # Retrieve the data using multiple threads
            station_data_future = executor.submit(dbContext.get_station_data)
            availability_data_future = executor.submit(
                dbContext.get_availability_data)
            weather_data_future = executor.submit(dbContext.get_weather_data)
            current_availability_future = executor.submit(
                dbContext.get_stations_availability)

            # Get the results from the futures
            stations = station_data_future.result()
            bikes = availability_data_future.result()
            weather = weather_data_future.result()
            current_availability = current_availability_future.result()

        return render_template('dashboard.html', weather=weather, bikes=bikes, stations=stations,
                               current_availability=json.dumps(current_availability))
    return redirect(url_for('index'))

@app.before_request
def before_request():
    g.user = None
    if 'user' in session:
        g.user = session['user']

@app.template_filter("tojson")
def tojson_filter(value):
    return json.dumps(value)

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


import argparse
import json
import logging
import os
import sys
from ..server import DynnameAPI
from ..config import load_config
from ..net import get_ips

def _get_cert_path(cert_dir, fqdn):
    if fqdn.endswith('.'):
        fqdn = fqdn[:-1]
    path = os.path.join(cert_dir, fqdn)
    if not os.path.exists(path):
        return None
    return path

logger = logging.getLogger(__name__)

parser = argparse.ArgumentParser(description='Command line tool for dynna.me')
parser.add_argument('-g', '--get', action='store_true',
                    help='Action: get proxy info')
parser.add_argument('-u', '--update', action='store_true',
                    help='Action: update proxy info')
parser.add_argument('--acme-validation', type=str, default=None,
                    help='ACME validation')
parser.add_argument('-d', '--debug', action='store_true',
                    help='Debug mode')
parser.add_argument('--cert-dir', type=str, default=None,
                    help='Path to the certificate directory')
parser.add_argument('-v', '--verbose', action='store_true',
                    help='Verbose mode')
parser.add_argument('-c', '--config', type=str, default='~/.dynname.json',
                    help='Path of the dynname config')

args = parser.parse_args(sys.argv[1:])

log_level = logging.DEBUG if args.verbose else logging.WARNING
LOG_FORMAT = '%(asctime)-15s %(levelname)s %(message)s'
logging.basicConfig(level=log_level, format=LOG_FORMAT)

config = load_config(args.config)
api = DynnameAPI(config)
if args.get:
    proxy = api.get_proxy() if 'proxy_id' in config else {}
    if args.cert_dir and 'fqdns' in proxy:
        proxy['cert_dirs'] = [_get_cert_path(args.cert_dir, fqdn) for fqdn in proxy['fqdns']]
    print(json.dumps(proxy))
elif args.update:
    proxy_ip = None
    if 'proxy_nic' in config and config['proxy_nic'] is not None:
        ips = get_ips()
        proxy_nic = config['proxy_nic']
        if proxy_nic in ips:
            proxy_ip = ips[proxy_nic]
        else:
            logger.warn(f'No nic: {proxy_nic}')
    if args.acme_validation is None and proxy_ip is None:
        logger.info('No properties to update')
        sys.exit(0)
    api.update_proxy(acme_validation=args.acme_validation, proxy_ip=proxy_ip)
else:
    print('Action not defined', file=sys.stderr)
    parser.print_help()