from typing import List, Dict, Any
import time
from stv.models.asynchronous.global_state import GlobalState
from stv.models.asynchronous.local_model import LocalModel
from stv.models.asynchronous.local_transition import LocalTransition, SharedTransition
from stv.models import SimpleModel

class GlobalModel:
    """
    Represents global model.

    :param local_models:
    :param reduction:

    :ivar _model:
    :ivar _local_models:
    :ivar _reduction:
    :ivar _states:
    :ivar _transitions:
    :ivar _dependent:
    :ivar _pre:
    :ivar _agents_count:
    :ivar _stack1:
    :ivar _stack2:
    :ivar _G:
    :ivar coalition:
    :ivar _stack1_dict:
    :ivar _transitions_count
    """

    def __init__(self, local_models: List[LocalModel], reduction: List[str]):
        self._model: SimpleModel = None
        self._local_models: List[LocalModel] = local_models
        self._reduction: List[str] = reduction
        self._states: List[GlobalState] = []
        self._transitions: List = []
        self._dependent: List[List[List[int]]] = []
        self._pre: List[List[List[LocalTransition]]] = []
        self._agents_count: int = 0
        self._states_dict: Dict[str, int] = dict()
        self._stack1: List[Any] = []
        self._stack2: List[int] = []
        self._G: List = []
        self.coalition: List = []
        self._stack1_dict: Dict[str, int] = dict()
        self._transitions_count: int = 0

    @property
    def model(self):
        """The model."""
        return self._model

    @property
    def states_count(self):
        return len(self._states)

    @property
    def transitions_count(self):
        return self._transitions_count

    def generate(self, reduction: bool = False):
        """
        Generates model.
        :param reduction: Should reductions be used.
        :return: None.
        """
        self._agents_count = len(self._local_models)
        self._model = SimpleModel(self._agents_count)
        self._add_to_stack(GlobalState.initial_state(self._agents_count))
        self._add_index_to_transitions()
        self._compute_dependent_transitions()
        self._compute_shared_transitions()
        if reduction:
            self._iter_por()
        else:
            self._compute()

    def _add_index_to_transitions(self):
        for agent_id in range(self._agents_count):
            for i in range(len(self._local_models[agent_id].transitions)):
                for j in range(len(self._local_models[agent_id].transitions[i])):
                    self._local_models[agent_id].transitions[i][j].i = i
                    self._local_models[agent_id].transitions[i][j].j = j

    def _compute_shared_transitions(self):
        replace = []
        for agent_id in range(self._agents_count):
            for i in range(len(self._local_models[agent_id].transitions)):
                for j in range(len(self._local_models[agent_id].transitions[i])):
                    transition = self._local_models[agent_id].transitions[i][j]
                    if not transition.shared:
                        continue

                    shared_transition = self._create_shared_transition(transition, agent_id)
                    shared_transition.transition_list.sort(key=lambda tran: tran.agent_id)
                    replace.append((agent_id, i, j, shared_transition))

        for rep in replace:
            agent_id, i, j, shared_transition = rep
            self._local_models[agent_id].transitions[i][j] = shared_transition

    def _create_shared_transition(self, transition: LocalTransition, agent_id: int) -> SharedTransition:
        shared_transition = SharedTransition(transition)
        for agent_id2 in range(self._agents_count):
            if agent_id == agent_id2:
                continue

            if self._local_models[agent_id2].has_action(transition.action):
                for transition2 in self._local_models[agent_id2].get_transitions():
                    if transition2.action == transition.action:
                        shared_transition.add_transition(transition2)
                        break

        return shared_transition

    def _available_transitions_in_state_for_agent(self, state: GlobalState, agent_id: int) -> List[LocalTransition]:
        """
        Computes a list of transitions available for the specified agent in the given state.
        :param state: Global state.
        :param agent_id: Agent identifier.
        :return: List of local transitions.
        """
        agent_state_id: int = state.local_states[agent_id]
        all_transitions: List[LocalTransition] = self._local_models[agent_id].private_transitions_from_state(
            agent_state_id)
        all_transitions += self._local_models[agent_id].shared_transitions_from_state(agent_state_id)
        return list(filter(lambda transition: transition.check_conditions(state), all_transitions))

    def _enabled_transitions_in_state(self, state: GlobalState) -> List[List[LocalTransition]]:
        """
        Computes all enabled transitions for the given global state.
        :param state:
        :return:
        """
        all_transitions = []
        for agent_id in range(len(self._local_models)):
            all_transitions.append(self._available_transitions_in_state_for_agent(state, agent_id))

        result = []
        for agent_id in range(self._agents_count):
            result.append(self._enabled_transitions_for_agent(agent_id, all_transitions))

        return result

    def _enabled_transitions_for_agent(self, agent_id: int, all_transitions: List[List[LocalTransition]]):
        """
        Computes enabled transitions for given agent based on the transitions from the global state.
        :param agent_id: Agent identifier.
        :param all_transitions: List containing all of the transitions going out from specific global state.
        :return:
        """
        result = []
        for transition in all_transitions[agent_id]:
            if not transition.shared:
                result.append(transition)
                continue

            if self._check_if_shared_transition_is_enabled(transition, agent_id, all_transitions):
                result.append(transition)

        return result

    def _check_if_shared_transition_is_enabled(self, transition: LocalTransition, agent_id: int,
                                               all_transitions: List[List[LocalTransition]]) -> bool:
        is_ok = True
        for agent_id2 in range(len(self._local_models)):
            if agent_id2 == agent_id:
                continue

            if self._local_models[agent_id2].has_action(transition.action):
                is_ok = False
                for transition2 in all_transitions[agent_id2]:
                    if transition2.shared and transition2.action == transition.action:
                        is_ok = True
                        break

            if not is_ok:
                return False

        return True

    def _enabled_transitions_in_state_single_item_set(self, state: GlobalState):
        enabled = self._enabled_transitions_in_state(state)
        result = set()
        for agent_id in range(self._agents_count):
            for transition in enabled[agent_id]:
                result.add(transition.to_tuple())
                if not transition.shared:
                    continue
                for agent_id2 in range(agent_id + 1, self._agents_count):
                    i = 0
                    for transition2 in enabled[agent_id2]:
                        if transition2.shared and transition2.action == transition.action:
                            enabled[agent_id2].pop(i)
                            break
                        i += 1
        return result

    def _new_state_after_private_transition(self, state: GlobalState, transition: LocalTransition):
        agent_id = transition.agent_id
        new_state = GlobalState.copy_state(state)
        new_state.set_local_state(agent_id, self._local_models[agent_id].get_state_id(transition.state_to))
        new_state.increment_counter(agent_id)
        new_state = self._copy_props_to_state(new_state, transition)
        return new_state

    def _new_state_after_shared_transition(self, state: GlobalState, actual_transition):
        new_state = GlobalState.copy_state(state)
        agents = []
        for act_tran in actual_transition:
            new_state.increment_counter(act_tran[0])
            new_state.set_local_state(act_tran[0], self._local_models[act_tran[0]].get_state_id(
                act_tran[1].state_to))
            new_state = self._copy_props_to_state(new_state, act_tran[1])
            agents.append(act_tran[0])
        return new_state, agents

    def _new_state_after_shared_transitions_list(self, state: GlobalState, transitions: List[LocalTransition]):
        new_state = GlobalState.copy_state(state)
        for transition in transitions:
            new_state.set_local_state(transition.agent_id,
                                      self._local_models[transition.agent_id].get_state_id(transition.state_to))
            new_state = self._copy_props_to_state(new_state, transition)
        return new_state

    def _compute_next_for_state(self, state: GlobalState, current_state_id: int):
        all_transitions = self._enabled_transitions_in_state(state)
        visited = []
        for agent_id in range(len(self._local_models)):
            self._compute_next_for_state_for_agent(state, current_state_id, agent_id, visited, all_transitions)

    def _compute_next_for_state_for_agent(self, state: GlobalState, current_state_id: int, agent_id: int, visited: [],
                                          all_transitions: []):
        for transition in all_transitions[agent_id]:
            if transition.shared and transition.action not in visited:
                visited.append(transition.action)
                actual_transition = [(agent_id, transition)]
                for n_a_id in range(agent_id + 1, len(self._local_models)):
                    for n_tr in all_transitions[n_a_id]:
                        if n_tr.shared and n_tr.action == transition.action:
                            actual_transition.append((n_a_id, n_tr))
                            break
                new_state, agents = self._new_state_after_shared_transition(state, actual_transition)
                new_state_id = self._add_state(new_state)
                self._add_transition(current_state_id, new_state_id, transition.action, agents)
            elif not transition.shared:
                new_state = self._new_state_after_private_transition(state, transition)
                new_state_id = self._add_state(new_state)
                self._add_transition(current_state_id, new_state_id, transition.action, [agent_id])

    def _copy_props_to_state(self, state: GlobalState, transition: LocalTransition) -> GlobalState:
        for prop in transition.props:
            if transition.props[prop] == "?":
                pass
            elif type(transition.props[prop]) is str:
                if transition.props[prop] in state.props:
                    state.set_prop(prop, state.props[transition.props[prop]])
            elif type(transition.props[prop]) is bool:
                if not transition.props[prop]:
                    state.remove_prop(prop)
                else:
                    state.set_prop(prop, transition.props[prop])
            else:
                state.set_prop(prop, transition.props[prop])
        return state

    def _state_find(self, state: GlobalState):
        if state.to_str() in self._states_dict:
            return self._states_dict[state.to_str()]

        return -1

    def _compute_dependent_transitions(self):
        for agent_id in range(self._agents_count):
            self._dependent.append([])
            agent_transitions = self._local_models[agent_id].get_transitions()
            for i in range(0, len(agent_transitions)):
                self._dependent[agent_id].append([agent_id])
                for agent2_id in range(self._agents_count):
                    if agent_id == agent2_id:
                        continue

                    if self._local_models[agent2_id].has_action(agent_transitions[i].action):
                        self._dependent[agent_id][i].append(agent2_id)

    def _is_in_G(self, state: GlobalState):
        for st in self._G:
            if st.equal(state):
                return True
        return False

    def _find_state_on_stack1(self, state: GlobalState):
        str_state = state.to_str()

        if str_state in self._stack1_dict:
            return self._stack1_dict[str_state]

        return -1

    def _add_to_stack(self, state: GlobalState):
        str_state = state.to_str()

        if str_state in self._stack1_dict:
            return False
        else:
            self._stack1.append(state)
            self._stack1_dict[state.to_str()] = len(self._stack1) - 1
            return True

    def _pop_from_stack(self):
        self._stack1_dict[self._stack1[-1].to_str()] = -1
        self._stack1.pop()

    def _dfs_por(self):
        """
        Recursive partial order reductions algorithm.
        :return: None.
        """
        g = self._stack1[-1]
        reexplore = False

        i = self._find_state_on_stack1(g)

        if i != -1 and i != len(self._stack1) - 1:
            if not self._stack2:
                depth = 0
            else:
                depth = self._stack2[-1]
            if i > depth:
                reexplore = True
            else:
                self._pop_from_stack()
                return

        if not reexplore and self._is_in_G(g):
            self._pop_from_stack()
        g_state_id = self._add_state(g)
        en_g = self._enabled_transitions_in_state_single_item_set(g)
        if len(en_g) > 0:
            if not reexplore:
                E_g = self._ample(g)
            if len(E_g) == 0:
                E_g = en_g
            if E_g == en_g:
                self._stack2.append(len(self._stack1))
            for tup in E_g:
                a = self._local_models[tup[0]].transitions[tup[1]][tup[2]]
                g_p = self._successor(g, a)
                g_p_state_id = self._add_state(g_p)
                self._add_transition(g_state_id, g_p_state_id, a.action, [a.agent_id])  # TODO agents ids
                if self._add_to_stack(g_p):
                    self._dfs_por()
        if len(self._stack2) == 0:
            depth = 0
        else:
            depth = self._stack2[-1]
        if depth == len(self._stack1):
            self._stack2.pop()
        self._pop_from_stack()

    def _iter_por(self):
        """
        Iterative partial order reduction algorithm.
        :return: None.
        """
        dfs_stack = [1]
        while len(dfs_stack) > 0:
            dfs = dfs_stack.pop()
            if dfs == 1:
                g = self._stack1[-1]
                reexplore = False
                i = self._find_state_on_stack1(g)
                if i != -1 and i != len(self._stack1) - 1:
                    if len(self._stack2) == 0:
                        depth = 0
                    else:
                        depth = self._stack2[-1]
                    if i > depth:
                        reexplore = True
                    else:
                        self._pop_from_stack()
                        return

                if not reexplore and self._is_in_G(g):
                    self._pop_from_stack()
                    return
                self._G.append(g)
                g_state_id = self._add_state(g)
                E_g = []
                en_g = self._enabled_transitions_in_state_single_item_set(g)
                if len(en_g) > 0:
                    if not reexplore:
                        E_g = self._ample(g)
                    if len(E_g) == 0:
                        E_g = en_g
                    if E_g == en_g:
                        self._stack2.append(len(self._stack1))
                    dfs_stack.append(-1)
                    for tup in E_g:
                        a = self._local_models[tup[0]].transitions[tup[1]][tup[2]]
                        g_p = self._successor(g, a)
                        g_p_state_id = self._add_state(g_p)
                        self._add_transition(g_state_id, g_p_state_id, a.action, [a.agent_id])
                        if self._add_to_stack(g_p):
                            dfs_stack.append(1)
            elif dfs == -1:
                if len(self._stack2) == 0:
                    depth = 0
                else:
                    depth = self._stack2[-1]
                if depth == len(self._stack1):
                    self._stack2.pop()
                self._pop_from_stack()

    def _ample(self, state: GlobalState):
        """
        Computes ample set for given state.
        :param state: Global state.
        :return: Ample set.
        """
        V = self._enabled_transitions_in_state_single_item_set(state)
        while V:
            alpha = V.pop()
            V.add(alpha)
            X = {alpha}
            U = {alpha}
            DIS = set()
            while X and not X.difference(V):
                DIS.update(self._enabled_for_x(X))
                X = self._dependent_for_x(X, DIS, U)
                U.update(X)
            if not X and not self._check_for_cycle(state, U):
                return U
            V.difference_update(U)
        return {}

    def _check_for_cycle(self, state: GlobalState, X: set):
        for tup in X:
            transition = self._local_models[tup[0]].transitions[tup[1]][tup[2]]
            successor_state = self._successor(state, transition)
            if self._find_state_on_stack1(successor_state) != -1:
                return True
        return False

    def _enabled_for_x(self, X):
        result = set()

        for tup in X:
            transition = self._local_models[tup[0]].transitions[tup[1]][tup[2]]
            if isinstance(transition, SharedTransition):
                for transition2 in transition.transition_list:
                    for tr in self._local_models[transition2.agent_id].get_transitions():
                        if tr.state_from != transition2.state_from:
                            result.add(tr.to_tuple())
            else:
                for tr in self._local_models[transition.agent_id].get_transitions():
                    if tr.state_from != transition.state_from:
                        result.add(tr.to_tuple())

        return result

    def _dependent_for_x(self, X, DIS, U):  # !!!!
        result = set()
        for tup in X:
            transition = self._local_models[tup[0]].transitions[tup[1]][tup[2]]
            if isinstance(transition, SharedTransition):
                for transition2 in transition.transition_list:
                    for tr in self._local_models[transition2.agent_id].get_transitions():
                        if tr.to_tuple() not in DIS and tr.to_tuple() not in U:
                            result.add(tr.to_tuple())
            else:
                for tr in self._local_models[transition.agent_id].get_transitions():
                    if tr.to_tuple() not in DIS and tr.to_tuple() not in U:
                        result.add(tr.to_tuple())

        return result

    def _successor(self, state: GlobalState, transition: LocalTransition):
        if not isinstance(transition, SharedTransition):
            return self._new_state_after_private_transition(state, transition)
        else:
            return self._new_state_after_shared_transitions_list(state, transition.transition_list)

    def _add_state(self, state: GlobalState):
        state_id = self._state_find(state)
        if state_id == -1:
            state_id = len(self._states)
            state.id = state_id
            self._states.append(state)
            self._states_dict[state.to_str()] = state_id

        state.id = state_id
        return state_id

    def _add_transition(self, state_from: int, state_to: int, action: str, agents: List[int]):
        while len(self._transitions) <= state_from:
            self._transitions.append([])

        self._transitions[state_from].append({'from': state_from, 'to': state_to, 'action': action, 'agents': agents})
        self._transitions_count += 1
        self._model.add_transition(state_from, state_to, self._create_list_of_actions(action, agents))

    def _create_list_of_actions(self, action: str, agents: List[int]):
        actions = ['' for _ in range(self._agents_count)]
        for agent_id in agents:
            actions[agent_id] = action
        return actions

    def _compute(self):
        """
        Compute global model.
        :return:
        """
        state = GlobalState.initial_state(len(self._local_models))
        self._states.append(state)
        i = 0
        while i < len(self._states):
            state = self._states[i]
            current_state_id = i
            i += 1

            self._compute_next_for_state(state, current_state_id)

    def agent_name_to_id(self, agent_name: str) -> int:
        for agent_id in range(len(self._local_models)):
            if self._local_models[agent_id].agent_name == agent_name:
                return agent_id
        return -1

    def agent_name_coalition_to_ids(self, agent_names: List[str]) -> List[int]:
        agent_ids = []
        for agent_name in agent_names:
            agent_ids.append(self.agent_name_to_id(agent_name))
        return agent_ids

    def walk(self):
        print("Simulation start")
        current_state_id = 0
        while True:
            print("Current state:")
            self._states[current_state_id].print()
            print("Transitions:")
            for i in range(0, len(self._transitions[current_state_id])):
                print(f"{i}: {self._transitions[current_state_id][i]}")

            id = int(input("Select transition: "))
            current_state_id = self._transitions[current_state_id][id]['to']

    def print_dependent_transitions(self):
        for agent_id in range(self._agents_count):
            print(f"Agent {self._local_models[agent_id].agent_name}")
            agent_transitions = self._local_models[agent_id].get_transitions()
            for i in range(0, len(agent_transitions)):
                print("Transition:")
                agent_transitions[i].print()
                print("Dependent:")
                for agent2_id in self._dependent[agent_id][i]:
                    print(f"{self._local_models[agent2_id].agent_name}")
                print()
            print()

    def print(self):
        for model in self._local_models:
            model.print()

    def set_coalition(self, coalition: List[str]):
        self.coalition = self.agent_name_coalition_to_ids(coalition)

if __name__ == "__main__":
    from stv.models.asynchronous.parser import GlobalModelParser

    file_name = "voting.txt"
    model = GlobalModelParser().parse(file_name)
    # model.parse("train_controller.txt")
    # model.parse("selene.txt")
    # model.print()
    coalition = ["Coercer1"]
    model.set_coalition(coalition)
    print(f"Coalition: {coalition}")
    start = time.process_time()
    model.generate(reduction=True)
    end = time.process_time()
    print()
    print(f"Model generated in {end - start} seconds.")
    print(f"Model has {model.states_count} states.")
    print(f"Model has {model.transitions_count} transitions.")
    print()
    # model.walk()


#!/usr/bin/env python3
import matplotlib.pyplot as plt
import sys, random
n = int(sys.argv[1])
seed = int(sys.argv[2])

random.seed(seed)
print(n + n)
for _ in range(n) :
    x = random.randint(-1000000, 1000000)
    y = random.randint(200000, 1000000)
    print(x, y)
    print(x, -y)


#I want coffee

import sys
import argparse
from torch import nn, optim
import torch
import matplotlib.pyplot as plt

from data import mnist
from model import MyAwesomeModel

class TrainOREvaluate(object):
    """ Helper class that will help launch class methods as commands
        from a single script
    """
    def __init__(self):
        parser = argparse.ArgumentParser(
            description="Script for either training or evaluating",
            usage="python main.py <command>"
        )
        parser.add_argument("command", help="Subcommand to run")
        args = parser.parse_args(sys.argv[1:2])
        if not hasattr(self, args.command):
            print('Unrecognized command')

            parser.print_help()
            exit(1)
        # use dispatch pattern to invoke method with same name
        getattr(self, args.command)()

    def validation(model, testloader, criterion):
        accuracy = 0
        test_loss = 0
        for images, labels in testloader:

            output = model.forward(images)
            test_loss += criterion(output, labels).item()

            ## Calculating the accuracy
            # Model's output is log-softmax, take exponential to get the probabilities
            ps = torch.exp(output)
            # Class with highest probability is our predicted class, compare with true label
            equality = (labels.data == ps.max(1)[1])
            # Accuracy is number of correct predictions divided by all predictions, just take the mean
            accuracy += equality.type_as(torch.FloatTensor()).mean()

        return test_loss, accuracy

    def train(self):
        print("Training day and night")
        parser = argparse.ArgumentParser(description='Training arguments')
        parser.add_argument('--lr', default=0.001)
        # add any additional argument that you want
        args = parser.parse_args(sys.argv[2:])
        print(args)

        # TODO: Implement training loop here
        model = MyAwesomeModel()
        train_set, test_set = mnist()

        criterion = nn.CrossEntropyLoss()
        optimizer = optim.Adam(model.parameters(), lr= float(args.lr))
        epochs=2
        print_every = 40
        tLoss= []

        steps = 0
        running_loss = 0
        for e in range(epochs):
            # Model in training mode, dropout is on
            model.train()
            for images, labels in train_set:
                steps += 1

                optimizer.zero_grad()

                output = model.forward(images)
                loss = criterion(output, labels)
                loss.backward()
                optimizer.step()

                running_loss += loss.item()

                if steps % print_every == 0:
                    # Model in inference mode, dropout is off
                    model.eval()

                    # Turn off gradients for validation, will speed up inference
                    with torch.no_grad():
                        test_loss, accuracy = TrainOREvaluate.validation(model, test_set, criterion)

                    print("Epoch: {}/{}.. ".format(e + 1, epochs),
                          "Training Loss: {:.3f}.. ".format(running_loss / print_every),
                          "Test Loss: {:.3f}.. ".format(test_loss / len(test_set)),
                          "Test Accuracy: {:.3f}".format(accuracy / len(test_set)))

                    tLoss.append(running_loss / print_every)
                    running_loss = 0

                    # Make sure dropout and grads are on for training
                    model.train()

        torch.save(model.state_dict(), 'checkpoint.pth')

        plt.plot(tLoss, label='Training Loss')

        plt.legend()
        plt.show()

    def evaluate(self):
        print("Evaluating until hitting the ceiling")
        parser = argparse.ArgumentParser(description='Evaluating arguments')
        parser.add_argument('--load_model_from', default="checkpoint.pth")
        # add any additional argument that you want
        args = parser.parse_args(sys.argv[2:])
        print(args)

        # TODO: Implement evaluation logic here
        if args.load_model_from:
            model = MyAwesomeModel()
            model.load_state_dict(torch.load(args.load_model_from))
            model.eval()

            accuracy = 0

            _, test_set = mnist()

           # with torch.no_grad():
            for itr, (images, labels) in enumerate(test_set):
                output = model.forward(images)

                ps = torch.exp(output)

                equality = (labels.data == ps.max(1)[1])
                # Accuracy is number of correct predictions divided by all predictions, just take the mean
                accuracy += equality.type_as(torch.FloatTensor()).mean()

            print("Test Accuracy: {:.3f}".format(accuracy / len(test_set)))

        else:
            print("There is no model to evaluate please train first...")

if __name__ == '__main__':
    TrainOREvaluate()


# Copyright: 2019, NLnet Labs and the Internet.nl contributors
# SPDX-License-Identifier: Apache-2.0
import os

from celery import Celery
from celery.schedules import crontab

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'internetnl.settings')

app = Celery('internetnl')

app.config_from_object('django.conf:settings')

app.autodiscover_tasks()

@app.task(bind=True)
def debug_task(self):
    print('Request: {0!r}'.format(self.request))

if app.conf.ENABLE_BATCH:
    app.conf.beat_schedule = {
        'run_batch': {
                'task': 'tasks.run_batch',
                'schedule': app.conf.BATCH_SCHEDULER_INTERVAL,
        }
    }
else:
    # Disable HoF when on batch mode, too much DB activity.
    app.conf.beat_schedule = {
        'generate_HoF': {
                'task': 'update_HoF_ranking',
                'schedule': crontab(hour='*', minute='*/10', day_of_week='*'),
        }
    }


# -*- coding:UTF-8 -*-
# Python的多线程由于全局线程锁的存在并不能实现真正的并行编程，但是Python中的多进程编程模式是可以实现这个目标的。
# 多进程模式下进行上下文切换的损耗要远远大于线程。进程间无法直接共享数据，需要通过Queue、Pipe或则Manager方式做进程间通信。

from multiprocessing import Process,Queue,Pool

import multiprocessing
import os
import time

#simple multiprocessing
# def info(title):
#     print('title:',title)
#     print('name:',__name__)
#     print('parent process:',os.getppid())
#     print('process:',os.getpid())
# def f(name):
#     info('function f')
#     print('hello',name)

#通过Queue可以实现多个进程间的数据共享，先进先出
#添加数据
# def f(q,data):
#     print('存入数据')
#     q.put(data)
#     print('存入完毕')
# #获取数据
# def out(q):
#     time.sleep(4)
#     print('取出数据')
#     print(q.get())
#     print('取出完毕')

# multiprocessing.Pipe()即管道模式，调用Pipe()返回管道的两端的Connection。
# Pipe的本质是进程之间的数据传递，而不是数据共享，这和socket有点像。
# pipe()返回两个连接对象分别表示管道的两端，每端都有send()和recv()方法。
# 如果两个进程试图在同一时间的同一端进行读取和写入那么，这可能会损坏管道中的数据。
# def consumer(pipe):
#     output_p,input_p=pipe
#     input_p.close() #消费者关闭管道的输入端
#     while True:
#         try:
#             print("writting....")
#             item=output_p.recv()
#             print(item)
#         except EOFError:
#             print("越界")
#             break
# def productor(sequence,input_p): ## sequence用于存放需要被消费的数据列表
#     for item in sequence:
#         input_p.send(item)  ##将数据发送到管道

#进程锁
# def job(v,num,l): #共享对象，新的数据，锁
#     l.acquire()
#     for _ in range(5):
#         time.sleep(0.1)
#         v.value += num
#         print(v.value)
#     l.release()
# def multicore():
#     l = multiprocessing.Lock() #创建锁对象
#     v = multiprocessing.Value('i',0) #定义共享内存
#     p1=multiprocessing.Process(target=job,args=(v,1,l))
#     p2=multiprocessing.Process(target=job,args=(v,3,l))
#     p1.start()
#     p2.start()
#     p1.join()
#     p2.join(0)

##线程池
def Foo(i):
    time.sleep(2)
    return i+100

def Bar(arg):
    print('-->exec done:',arg)

# apply() 同步执行（串行）
# apply_async() 异步执行（并行）
# terminate() 立刻关闭进程池
# join() 主进程等待所有子进程执行完毕。必须在close或terminate()之后。
# close() 等待所有进程结束后，才关闭进程池

if __name__ == '__main__':
    #simple multiprocessing
    # print('main line:',os.getpid())
    # p = Process(target=f,args=('树先生',))
    # p.start()
    # p.join()

    #通过Queue可以实现多个进程间的数据共享
    # q=Queue()#创建实例
    # p = Process(target=f,args=(q,[1,2,3]))
    # p.start()
    # p.join()
    # p1 = Process(target=out,args=(q,))
    # p1.start()
    # p1.join()

    #pipe()数据传输
    # (output_p,input_p) = multiprocessing.Pipe(True)
    # #create consumer process
    # con_s=multiprocessing.Process(target=consumer,args=((output_p,input_p),))
    # con_s.start()
    #  #关闭生产者中的输出管道
    # output_p.close()
    # print("生产者关闭")
    # #生产项目
    # sequence=[1,2,3,4]
    # productor(sequence,input_p)
    # #关闭输入管道，表示完成
    # input_p.close()
    # #等待使用者进程关闭
    # con_s.join()

    #进程锁，与线程锁类似
    #multicore()

    #线程池
    pool = Pool(5)  #允许进程池同时放入5个进程
    for i in range(10):
        pool.apply_async(func=Foo, args=(i,),callback=Bar)  #func子进程执行完后，才会执行callback，否则callback不执行（而且callback是由父进程来执行了）

    print('end')
    pool.close()
    print('线程池关闭')
    pool.join() #主进程等待所有子进程执行完毕。必须在close()或terminate()之后。
    print('子线程结束')


# -*- coding: utf-8 -*-
import re
import os
import json
import time
import shutil
import datetime

import codecs
import requests
from .settings import BASE_DIR
from .tools import dir_maker, remove_item_re, remove_ntr_re, get_ch

# Original
class Fk5378Pipeline(object):
    def process_item(self, item, spider):
        return item

# Disk txt file
class PathPipeline(object):
    def __init__(self):
        self.file = None

    def process_item(self, item, spider):
        sys_item = eval(item['sys_item'])
        rsc_item = eval(item['rsc_item'])

        # CLEANER
        for k,v in rsc_item.items():
            rsc_item[k] = remove_ntr_re(v)
        rsc_item['tags'] = get_ch(rsc_item.get('tags'))

        # BUILD
        _data = {
            '爬虫作者': '曾影穹(vcrting@163.com)'
        }
        for k in rsc_item:
            _data[k] = rsc_item[k]
        for k in sys_item:
            _data[k] = sys_item[k]
        data_str = json.dumps(_data, ensure_ascii=False)

        # PATH
        save_path = os.path.join(item['save_path'], rsc_item['title'])
        if dir_maker(save_path):
            # FILE
            self.file = codecs.open(
                os.path.join(
                    save_path,
                    rsc_item['title'] + '.json'
                ),
                'wb',
                encoding='utf-8'
            )
            self.file.write(data_str)

            # IMG
            image = requests.get(rsc_item['cover']).content
            image_save_path = os.path.join(
                save_path,
                rsc_item['title'] + '_' + rsc_item['cover'][-6:]
            )
            with open(image_save_path, 'wb') as f:
                f.write(image)

        return item

    def close_spider(self, spider):
        self.file.close()


import h5py, pytz, csv, numpy
import matplotlib.pyplot as plt
from datetime import datetime
from pytz import timezone
from scipy.signal import medfilt

FILENAME = '1541962108935000000_167_838.h5'
CERN_TIMEZONE = timezone('Europe/Zurich')
UNIXTIME = FILENAME[:18]

def task_1_time():
    utc = pytz.utc
    utc_dt = utc.localize(datetime.utcfromtimestamp(int(UNIXTIME)/100000000))
    cern_dt = utc_dt.astimezone(CERN_TIMEZONE)
    print('UTC datetime: ', utc_dt, '\nCERN datetime: ', cern_dt)

def task_2_csv():
    f = h5py.File(FILENAME, 'r')

    csv_data = []
    def getData(name, obj):
        if isinstance(obj, h5py.Dataset):
            try:
                csv_data.append([name, obj.size, obj.shape, obj.dtype, 'Dataset'])
            except TypeError: # There are a few data type for which h5py does not have a type
                csv_data.append([name, obj.size, obj.shape, None, 'Dataset'])
        elif isinstance(obj, h5py.Group):
            csv_data.append([name, None, None, None, 'Group'])
    f.visititems(getData)

    with open("%s.csv" % str(UNIXTIME), "w") as csv_file:
        writer = csv.writer(csv_file)
        writer.writerow(['name', 'size', 'shape', 'dtype', 'type'])
        writer.writerows(csv_data)
    f.close()

def task_3_img():
    f = h5py.File(FILENAME, 'r')

    dataset = 'AwakeEventData/XMPP-STREAK/StreakImage/streakImageData'
    dataset_height = 'AwakeEventData/XMPP-STREAK/StreakImage/streakImageHeight'
    dataset_width = 'AwakeEventData/XMPP-STREAK/StreakImage/streakImageWidth'

    dataset_2d = f[dataset][()].reshape(f[dataset_height][0], f[dataset_width][0])
    filtered_img = medfilt(dataset_2d)
    plt.imshow(filtered_img)
    plt.savefig("%s.png" % str(UNIXTIME))
    # plt.show()
    f.close()

if __name__ == "__main__":
    task_1_time()
    task_2_csv()
    task_3_img()


# -*- coding: utf-8 -*-
##----------------------------------------------------------------------
## fm.reportoutages
##----------------------------------------------------------------------
## Copyright (C) 2007-2015 The NOC Project
## See LICENSE for details
##----------------------------------------------------------------------

## Python modules
import datetime
from collections import defaultdict
## Django modu;es
from django import forms
## NOC modules
from noc.fm.models.outage import Outage
from noc.sa.models.managedobject import ManagedObject
from noc.lib.app.simplereport import SimpleReport, TableColumn
from noc.lib.dateutils import total_seconds
from noc.lib.nosql import Q

class ReportForm(forms.Form):
    duration = forms.ChoiceField(choices=[
        (86400, "1 day"),
        (7 * 86400, "1 week"),
        (30 * 86400, "1 month")
    ])

class ReportOutagesApplication(SimpleReport):
    title = "Outages"

    form = ReportForm

    def get_data(self, duration, **kwargs):
        now = datetime.datetime.now()
        d = datetime.timedelta(seconds=int(duration))
        b = now - d
        outages = defaultdict(list)
        otime = defaultdict(int)
        q = Q(start__gte=b) | Q(stop__gte=b) | Q(stop__exists=False)
        for o in Outage.objects.filter(q):
            start = max(o.start, b)
            stop = o.stop if o.stop else now
            outages[o.object] += [o]
            otime[o.object] = total_seconds(stop - start)
        td = total_seconds(d)
        # Load managed objects
        mos = list(otime)
        chunk = 500
        mo = {}
        while mos:
            for o in ManagedObject.objects.filter(id__in=mos[:chunk]):
                mo[o.id] = o
            mos = mos[chunk:]
        r = []
        for o in sorted(otime, key=lambda x: -otime[x]):
            m = mo.get(o)
            if not m:
                continue  # Hanging Outage
            dt = otime[o]
            downtime = "%02d:%02d:%02d" % (
                (dt // 3600) % 24,
                (dt // 60) % 60,
                dt % 60
            )
            if dt >= 86400:
                downtime = "%dd %s" % (dt // 86400, downtime)
            r += [(
                m.name,
                m.profile_name,
                m.platform,
                m.is_managed,
                m.get_status(),
                downtime,
                float(td - dt) * 100 / td,
                len(outages[o])
            )]

        return self.from_dataset(
            title=self.title,
            columns=[
                "Object", "Profile", "Platform",
                TableColumn("Managed", format="bool"),
                TableColumn("Status", format="bool"),
                TableColumn("Downtime", align="right"),
                TableColumn("Availability", align="right", format="percent"),
                TableColumn("Downs", align="right", format="integer")
            ],
            data=r,
            enumerate=True
        )


import pandas as pd

#导入Excel文件指定列数据（“买家会员名”和“收货地址”）
df = pd.read_excel('mrbooks.xls',usecols=['买家会员名','收货地址'])
#使用split方法分割“收货地址”
series=df['收货地址'].str.split(' ',expand=True)
df['省']=series[0]
df['市']=series[1]
df['区']=series[2]
df1=df.head()


#!/usr/bin/python3
"""6. Coordinates of a square
    Write a class Square that defines a square by: (based on 5-square.py)
    Private instance attribute: size:
    -   property def size(self): to retrieve it
    -   property setter def size(self, value): to set it:
        -   size must be an integer, otherwise raise a TypeError exception
            with the message size must be an integer
        -   if size is less than 0, raise a ValueError exception with the
            message size must be >= 0
    Private instance attribute: position:
    -   property def position(self): to retrieve it
    -   property setter def position(self, value): to set it:
        -   position must be a tuple of 2 positive integers, otherwise
            raise a TypeError exception with the message position must be
            a tuple of 2 positive integers
    Instantiation with optional size and optional position: def __init__
    (self, size=0, position=(0, 0)):
    Public instance method: def area(self): that returns the current square
    area
    Public instance method: def my_print(self): that prints in stdout the
    square with the character #:
    -   if size is equal to 0, print an empty line
    -   position should be use by using space - Don’t fill lines by spaces
        when position[1] > 0
    You are not allowed to import any module
"""

class Square:
    """Square
        Init function to instance square size
        validate if a integer and if less than 0

    """
    def __init__(self, size=0, position=(0, 0)):
        """__init__ method
            Args:
                size: size of square, must be integer >= 0
            Attributes:
                size: validate if a correct integer value
        """
        self.size = size
        self.position = position

    def area(self):
        """area method
           Returns:
                The value of area of size of square -> A = S * S
        """
        return self.__size * self.__size

    @property
    def size(self):
        """size - getter method
            Returns:
                 the value of size
        """
        return self.__size

    @size.setter
    def size(self, value):
        """size - setter method
            validates the data if a valid integer
        """
        if type(value) != int:
            raise TypeError("size must be an integer")
        elif value < 0:
            raise ValueError("size must be >= 0")
        self.__size = value

    @property
    def position(self):
        """Position - getter method
                    Returns:
                         the position tuple
        """
        return self.__position

    @position.setter
    def position(self, value):
        """position - setter method
                    validates the data if a valid tuple
        """
        if type(value) != tuple:
            raise TypeError("position must be a tuple of 2 positive integers")
        elif len(value) != 2:
            raise TypeError("position must be a tuple of 2 positive integers")
        elif type(value[0]) != int or type(value[1]) != int:
            raise TypeError("position must be a tuple of 2 positive integers")
        elif len(value) != 2 or value[0] < 0 or value[1] < 0:
            raise TypeError("position must be a tuple of 2 positive integers")
        self.__position = value

    def my_print(self):
        """my_print
                print the square
        """
        if self.__size <= 0:
            print()
            return
        print("\n" * self.position[1], end="")
        for i in range(self.__size):
            print(" " * self.position[0], end="")
            for j in range(self.__size):
                print("#", end="")
            print()


import re
import urllib2
from bs4 import BeautifulSoup

class RefWords(object):
    def __init__(self, word):
        content = urllib2.urlopen("http://dictionary.reference.com/browse/{0}".format(word), timeout=10).read()
        self.content = BeautifulSoup(content, "html.parser")

    def get_ipa(self):
        ipa = self.content.find('span', {'class': 'pron ipapron'})
        if ipa == None:
            return ""
        else:
            return ipa.string

    def get_def(self):
        def_str = u""
        soup = self.content.find('div', {'class': 'source-box'})
        for row in soup.find_all('section', {'class': 'def-pbk'}):
            text = row.get_text().replace('\n', '').replace('\r', '')
            count = 0
            for line in re.split('\d+\.(?!\d)', text):
                if line.strip() == "":
                    continue
                if count == 0:
                    def_str += line + '\n'
                else:
                    def_str += u"{0}. {1}\n".format(count, line)
                count += 1
            def_str += "\n"
        return def_str


'''
To run:

    python plot.py

in this directory, and navigate to:

    http://localhost:5000

'''
#!/usr/bin/env python

from __future__ import print_function

import flask

import numpy as np
import pandas as pd
import datetime
import urllib
import ijson

from bokeh.embed import components
from bokeh.plotting import *
from bokeh.models import CustomJS,HoverTool,ColumnDataSource, PrintfTickFormatter
from bokeh.models.widgets import Slider, TextInput
from bokeh.resources import INLINE
from bokeh.util.string import encode_utf8

from collections import OrderedDict

app = flask.Flask(__name__)

url_default='http://msi.mcgill.ca/GSoC_NANOGrav/pulsar_data_test.json'

def getitem(obj, item, default):
    if item not in obj:
        return default
    else:
        return obj[item]

@app.route("/")
def polynomial():
    """ Very simple embedding of a polynomial chart

    """

    # Grab the inputs arguments from the URL
    # This is automated by the button
    args = flask.request.args

    # Get all the form arguments in the url with defaults
    _source=getitem(args,'_source',url_default)
    _size=int(getitem(args,'size','5'))
    favcolor=getitem(args,'favcolor','#1F77B4')
    bcolor=getitem(args,'bcolor','#F5F5DC')
    _shape=getitem(args,'_shape','circle')
    lcolor=getitem(args,'lcolor','#FCB938')
    toggle=bool(getitem(args,'toggle',''))

    #Parsing json

    DEFAULT_TICKERS = ['TOAs','RawProfiles', 'Period', 'PeriodDerivative', 'DM', 'RMS', 'Binary']
    TOOLS = "tap,resize,crosshair,pan,wheel_zoom,box_zoom,reset,box_select,lasso_select,previewsave,hover"

    Pulsar,TOAs,RawProfiles, Period, PeriodDerivative, DM, RMS, Binary=[],[],[],[],[],[],[],[]
    url_location=urllib.urlopen(_source)
    for item in ijson.items(url_location,"item"):
        Pulsar.append(str(item["Pulsar"]))
        TOAs.append(float(item["TOAs"]))
        RawProfiles.append(int(item["Raw Profiles"]))
        Period.append(float(item["Period"]))
        PeriodDerivative.append(float(item["Period Derivative"]))
        DM.append(str(item["DM"]))
        RMS.append(str(item["RMS"]))
        if (item["Binary"]=="Y"):
            Binary.append("Yes")
        else:
            Binary.append("No")

    #Create a plot periodvs period derivative

    s1 = ColumnDataSource(
    data=dict(
        Pulsar=Pulsar,
        TOAs=TOAs,
        RawProfiles=RawProfiles,
        Period=Period,
        PeriodDerivative=PeriodDerivative,
        DM=DM,
        RMS=RMS,
        Binary=Binary,
        )
    )

    p1 = figure(plot_width=600, plot_height=600,
               title="Period vs Period Derivative", y_axis_type="log" ,y_range=[min(PeriodDerivative)-min(PeriodDerivative)/5, max(PeriodDerivative)+max(PeriodDerivative)/5],x_range=[min(Period)-min(Period)/5, max(Period)+max(Period)/5],x_axis_label='Period[s]', y_axis_label='Period Derivative[s/s]',tools=TOOLS)
    p1.background_fill_color = bcolor
    p1.background_fill_alpha = "0.5"
    getattr(p1,_shape)('Period', 'PeriodDerivative', legend="period deri",color=favcolor,size=_size, source=s1)
    #p1.circle('Period', 'PeriodDerivative', legend="period deri",color=favcolor,size=_size, source=s1)
    p1.xaxis.axis_label_text_font_size = "15pt"
    p1.yaxis.axis_label_text_font_size = "15pt"
    #p1.xaxis[0].formatter = PrintfTickFormatter(format="s")
    #p1.yaxis[0].formatter = PrintfTickFormatter(format=" s/s")

    #Toggle Line
    #if getattr(p1,line,None):
    #   getattr(p1,line)('Period', 'PeriodDerivative',legend="period deri",line_dash=[4, 4], line_color=lcolor, line_width=1,source=s1)
    p1.line('Period', 'PeriodDerivative',legend="period deri",line_dash=[4, 4], line_color=lcolor, line_width=1,source=s1,visible=toggle)

    # Custom data source for selected points
    s2 = ColumnDataSource(
        data=dict(
            Pulsar=[],
            TOAs=[],
            RawProfiles=[],
            Period=[],
        PeriodDerivative=[],
        DM=[],
        RMS=[],
        Binary=[],
        )
    )

    p2= figure(plot_width=600, plot_height=600,
               title=" Selected points from Period vs Period Derivative", y_axis_type="log" ,y_range=[min(PeriodDerivative)-min(PeriodDerivative)/10, max(PeriodDerivative)+max(PeriodDerivative)/10],x_range=[min(Period)-min(Period)/10, max(Period)+max(Period)/10],x_axis_label='Period[s]', y_axis_label='Period Derivative[s/s]',tools=TOOLS)
    p2.xaxis.axis_label_text_font_size = "15pt"
    p2.yaxis.axis_label_text_font_size = "15pt"

    p2.circle('Period', 'PeriodDerivative', legend="period deri",alpha=1.2, source=s2)

    s1.callback = CustomJS(args=dict(s2=s2), code="""
            var inds = cb_obj.get('selected')['1d'].indices;
            var d1 = cb_obj.get('data');
            var d2 = s2.get('data');
            d2['Pulsar'] = []
            d2['TOAs'] = []
            d2['RawProfiles'] = []
            d2['Period'] = []
            d2['PeriodDerivative'] = []
            d2['DM'] = []
            d2['RMS'] = []
            d2['Binary'] = []
            for (i = 0; i < inds.length; i++) {
                d2['Pulsar'].push(d1['Pulsar'][inds[i]])
                d2['TOAs'].push(d1['TOAs'][inds[i]])
                d2['RawProfiles'].push(d1['RawProfiles'][inds[i]])
                d2['Period'].push(d1['Period'][inds[i]])
                d2['PeriodDerivative'].push(d1['PeriodDerivative'][inds[i]])
                d2['DM'].push(d1['DM'][inds[i]])
                d2['RMS'].push(d1['RMS'][inds[i]])
                d2['Binary'].push(d1['Binary'][inds[i]])

            }
            s2.trigger('change');
      """  )

    hover = p1.select(dict(type=HoverTool))
    hover.tooltips = OrderedDict([
        ("Pulsar's Name", '@Pulsar'),
        ('TOAs', '@TOAs'),
        ('RawProfiles', '@RawProfiles'),
        ('Period[s]', '@Period'),
        ('PeriodDerivative[s/s]', '@PeriodDerivative'),
        ('DM[pc/cc]', '@DM'),
        ('RMS[us]', '@RMS'),
        ('Binary', '@Binary'),
    ])

    js_resources = INLINE.render_js()
    css_resources = INLINE.render_css()

    #Setting up layout
    layout = hplot(p1, p2)

    script, div = components(layout,INLINE)
    html = flask.render_template(
        'embed.html',
        plot_script=script,
        plot_div=div,
        js_resources=js_resources,
        css_resources=css_resources,
        _source=_source,
        Pulsar=Pulsar,
        _shape=_shape,
        favcolor=favcolor,
        bcolor=bcolor,
        _size=_size,
        lcolor=lcolor,
        toggle=toggle
    )
    return encode_utf8(html)

if __name__ == "__main__":
    print(__doc__)
    app.run()


import logging
import requests
import sys
import time
from multiprocessing import Value
import concurrent.futures
from loader import settings

logging.basicConfig(level=logging.INFO)
log = logging.getLogger(__name__)

def fetch_bootstrap_ads():
    feed_url = settings.LA_BOOTSTRAP_FEED_URL

    log.info('fetch_bootstrap_ad_ids - fetching ads from %s' % feed_url)

    distinct_items = None
    try:
        r = requests.get(feed_url)
        r.raise_for_status()
        json_result = r.json()
        if json_result and 'idLista' in json_result:
            items = json_result['idLista']
            distinct_items = list({v['annonsId']: v for v in items}.values())
    except requests.exceptions.RequestException as e:
        log.error("Failed to read from %s" % feed_url, e)
    return distinct_items

def is_ad_included(ad_id_item, last_ts, exclude_ids):
    if ad_id_item['uppdateradTid'] == last_ts and \
            str(ad_id_item['annonsId']) in exclude_ids:
        return False
    else:
        return True

def fetch_updated_ads(last_ts, exclude_ids):
    exclude_ids = set(exclude_ids)
    feed_url = settings.LA_FEED_URL + str(last_ts)
    log.info('fetch_updated_ads - fetching ads from %s' % feed_url)

    distinct_items = None
    try:
        r = requests.get(feed_url)
        r.raise_for_status()
        json_result = r.json()
        if json_result and 'idLista' in json_result:
            items = json_result['idLista']
            distinct_items = list({v['annonsId']: v for v in items
                                   if is_ad_included(v, last_ts, exclude_ids)}.values())
    except requests.exceptions.RequestException as e:
        log.error("Failed to read from %s" % feed_url, e)

    return distinct_items

def clean_stringvalues(value):
    if isinstance(value, dict):
        value = {clean_stringvalues(k): clean_stringvalues(v) for k, v in value.items()}
    elif isinstance(value, list):
        value = [clean_stringvalues(v) for v in value]
    elif isinstance(value, str):
        value = ''.join([i if ord(i) > 0 else '' for i in value])
    return value

def fetch_ad_details(ad_id, ts, url):
    fail_count = 0
    fail_max = 10
    detail_url = url + str(ad_id)
    while True:
        try:
            r = requests.get(detail_url, timeout=10)
            r.raise_for_status()
            ad = r.json()
            if ad:
                ad['id'] = str(ad['annonsId'])
                ad['updatedAt'] = ts
                ad['expiresAt'] = ad['sistaPubliceringsdatum']
                if 'kontaktpersoner' in ad:
                    del ad['kontaktpersoner']
                if 'organisationsnummer' in ad and \
                        len(ad.get('organisationsnummer', '').strip()) > 9:
                    orgnr = ad['organisationsnummer']
                    significate_number_position = 4 if len(orgnr) == 12 else 2
                    if int(orgnr[significate_number_position]) < 2:
                        ad['organisationsnummer'] = None
                clean_ad = clean_stringvalues(ad)
                return clean_ad
        except requests.exceptions.ConnectionError as e:
            fail_count += 1
            time.sleep(0.3)
            log.warning("Unable to load data from %s - Connection error, try %s" % (detail_url, fail_count))
            if fail_count >= fail_max:
                log.error("Failed to continue loading data from %s - Connection" %
                          detail_url, e)
                sys.exit(1)
        except requests.exceptions.Timeout as e:
            fail_count += 1
            time.sleep(0.3)
            log.warning("Unable to load data from %s - Timeout, try %d" % (detail_url, fail_count))
            if fail_count >= fail_max:
                log.error("Failed to continue loading data from %s - Timeout" %
                          detail_url, e)
                sys.exit(1)
        except requests.exceptions.RequestException as e:
            fail_count += 1
            time.sleep(0.3)
            log.warning("Unable to fetch data at %s, try %d" % (detail_url, fail_count))
            if fail_count >= fail_max:
                log.error("Failed to fetch data at %s, skipping" % detail_url, e)
                raise e

def execute_calls(ad_datas, details_url, parallelism):
    parallelism = int(parallelism)
    log.info('Multithreaded fetch ad details with %s processes' % str(parallelism))

    global counter
    counter = Value('i', 0)
    result_output = {}
    error_ids = []
    not_found_ids = []
    # with statement to ensure threads are cleaned up promptly
    with concurrent.futures.ThreadPoolExecutor(max_workers=parallelism) as executor:
        # Start the load operations
        future_to_fetch_result = {
            executor.submit(fetch_ad_details, ad_data['annonsId'],
                            ad_data['uppdateradTid'], details_url): ad_data for
            ad_data in ad_datas}
        for future in concurrent.futures.as_completed(future_to_fetch_result):
            input_data = future_to_fetch_result[future]
            try:
                detailed_result = future.result()
                result_output[detailed_result['annonsId']] = detailed_result
                # += operation is not atomic, so we need to get a lock for counter:
                with counter.get_lock():
                    # log.info('Counter: %s' % counter.value)
                    counter.value += 1
                    if counter.value % 100 == 0:
                        log.info("Multithreaded fetch ad details - Processed %s docs" %
                                 (str(counter.value)))
            except requests.exceptions.HTTPError as exc:
                status_code = exc.response.status_code
                error_message = 'Fetch ad details call generated an exception: %s' % \
                    (str(exc))
                log.error(error_message)
                if status_code == 404:
                    # This exception will happen if the list of updated ads contains ads
                    # with 'avpublicerad' == false even though the ad does not exist when
                    # trying to fetch the ad details (wrong status in the list).
                    not_found_ids.append(input_data)
                else:
                    error_ids.append(input_data['annonsId'])
            except Exception as exc:
                error_message = 'Fetch ad details call generated an exception: %s' % \
                    (str(exc))
                log.error(error_message)
                error_ids.append(input_data['annonsId'])

    return result_output, error_ids, not_found_ids


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

def main():
    file_name = "sim.c"
    executable_name = "run_sim"
    full_executable_path = os.getcwd() + "/" + executable_name
    subprocess.call(["gcc", "-lm", file_name, "-pthread", "-o", executable_name])

    sim_args = [(2,7),(2,9),(4,7),(4,9),(6,7),(6,9)]

    plot_file_name = "plot_data.csv"
    table_file_name = "table_data.csv"

    column_labels = ["(N,M)","Total Arrival", "Total Rejections", "Rejection Ratio",
                    "Average\nWait Time", "Max Waiting\nLine", "Max Waiting\nLine Time"]
    df_table = pd.DataFrame(columns = column_labels)

    arrivals_df_dict = {}
    waiting_df_dict = {}
    rejections_df_dict = {}

    for arg_tuple in sim_args:
        N = arg_tuple[0]
        M = arg_tuple[1]
        subprocess.call([full_executable_path, "-N " + str(N), "-M " + str(M)], stdout=open(os.devnull, 'wb'))

        # If MAXPERCAR == 7, then save the plot data.
        if arg_tuple[1] == 7:
            df_plots = pd.read_csv(plot_file_name)

            arrivals_list = df_plots.ix[:,0]
            rejects_list = df_plots.ix[:,1]
            waiting_list = df_plots.ix[:,2]

            arrivals_df_dict[arg_tuple] = arrivals_list
            waiting_df_dict[arg_tuple] = waiting_list
            rejections_df_dict[arg_tuple] = rejects_list

        # Generate and save the table.
        table_data_file = open(table_file_name, "r")
        table_values = ((table_data_file.readline()).strip()).split(",")
        row_label = "N=" +  str(N) + ",M=" + str(M)

        # This is a bit sloppy, but I struggled with getting the rows updated
        # correctly without doing it explicitly
        new_table_values = [row_label, table_values[2], table_values[3], table_values[4], table_values[5], table_values[6], table_values[7]]
        df_table.loc[row_label] = new_table_values

    make_plots(arrivals_df_dict, waiting_df_dict, rejections_df_dict)
    make_table(df_table)

# make_plots()
def make_plots(arrivals_df_dict, waiting_df_dict, rejections_df_dict):
    x_axis = np.linspace(0,600,600)

    arrivals_fig = plt.figure()
    ax0 = arrivals_fig.add_subplot(111)
    for key, value in arrivals_df_dict.items():
        ax0.plot(x_axis, value, label=key)
    ax0.legend(loc=2)
    ax0.set_title("Arrivals Plot")
    ax0.set_xlabel("Time (minutes)")
    ax0.set_ylabel("Num. of Arrivals")
    ax0.grid(True)
    plt.savefig("arrivals-plot.png", bbox_inches='tight')

    waiting_fig = plt.figure()
    ax1 = waiting_fig.add_subplot(111)
    for key, value in waiting_df_dict.items():
        ax1.plot(x_axis, value, label=key)
    ax1.legend(loc=2)
    ax1.set_title("Waiting Line Plot")
    ax1.set_xlabel("Time (minutes)")
    ax1.set_ylabel("Num of People Waiting")
    ax1.grid(True)
    plt.savefig("waiting-plot.png", bbox_inches='tight')

    rejection_fig = plt.figure()
    ax2 = rejection_fig.add_subplot(111)
    for key, value in rejections_df_dict.items():
        ax2.plot(x_axis, value, label=key)
    ax2.legend(loc=2)
    ax2.set_title("Rejections Plot")
    ax2.set_xlabel("Time (minutes)")
    ax2.set_ylabel("Num of People Rejected")
    ax2.grid(True)
    plt.savefig("rejections-plot.png", bbox_inches='tight')

# make_table()
def make_table(df_table):
    # Making table of values
    fig, ax = plt.subplots()
    fig.patch.set_visible(False)
    ax.axis('off')

    # The bbox and tight_layout numbers had to be carefully found.
    table = ax.table(cellText = df_table.values, colLabels=df_table.columns, loc='center', bbox=[-0.38, -0.3, 1.4, 0.275]) # first value is left shift
    table.auto_set_font_size(False)
    table.set_fontsize(8)
    fig.tight_layout(rect=[0.2, 0.8, 1.02, 2.2]) # 3rd value is width of cells
    plt.savefig("table.png")

    plt.show()

if __name__ == "__main__":
    main()


from django.urls import path, include
from rest_framework.routers import DefaultRouter
from .viewsets import (
    CustomTextViewSet,
    DescriptionViewSet,
    HomePageViewSet,
    InventoryViewSet,
    LocationViewSet,
    LokationViewSet,
    NameViewSet,
    StoresViewSet,
    StorezViewSet,
)

from home.api.v1.viewsets import (
    SignupViewSet,
    LoginViewSet,
    HomePageViewSet,
    CustomTextViewSet,
)

router = DefaultRouter()
router.register("signup", SignupViewSet, basename="signup")
router.register("login", LoginViewSet, basename="login")
router.register("customtext", CustomTextViewSet)
router.register("homepage", HomePageViewSet)
router.register("location", LocationViewSet)
router.register("stores", StoresViewSet)
router.register("inventory", InventoryViewSet)
router.register("storez", StorezViewSet)
router.register("name", NameViewSet)
router.register("lokation", LokationViewSet)
router.register("description", DescriptionViewSet)

urlpatterns = [
    path("", include(router.urls)),
]


#!/usr/bin/env python2
# -*- coding: utf-8 -*-
"""
Created on Mon May 22 13:51:06 2017

@author: leo
"""
import numpy as np
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
import json

#with open("results_paths.json",'r') as fp:
#    p = json.load(fp)

def heat_map(plotPath, fn, thevar="DSsc"):

    filename = plotPath + fn
    data = np.loadtxt(filename, skiprows=1, delimiter=',')

    with open(filename) as f:
        lab = f.readline()
        lab = lab.replace(' ','') #old versions have spaces...
        labels = lab.split(',')
        data.shape = data.size
        data.dtype = [(l, float) for l in labels]
        data.sort(order=(labels[1], labels[3]))

    x = data['vel']
    y = data['gmax']

    sqsize = int(np.sqrt(len(data)))

    X = np.reshape(x,(-1,sqsize))
    Y = np.reshape(y,(-1,sqsize))

    SACS = fn.split('_')
    sacNames = []
    for i in range(len(SACS)):
        # if we found a number, it is a cell (TRUE), otherwise, FALSE
        sacNames.append(SACS[i].isdigit())

    sac_number = []
    for i in range(len(sacNames)):
        if(sacNames[i]):  # If true, I will save the sac number
            sac_number.append(SACS[i])

    plots = range(len(sac_number))  # how many plots? One for each SAC

    for sac in plots:
        z = data['sc%dp' % sac] * data['DSsc%d' % sac]
    #    z = data['DSsc%d' % sac]
        # z = data['sc%dp' % sac] * data['DSsc%d' % sac]
        z = data[thevar + str(sac)]
        # the following number must be concordant with the stimuli speed
        Z = np.reshape(z, (-1, sqsize))
        fig, ax = plt.subplots()
        # plt.clf()
        plt.title('SAC number: ' + str(sac_number[plots.index(sac)]))
        cax = ax.pcolor(X, Y, Z, vmin=0, vmax=.5)
        ax.set_xscale('log')
        ax.set_yscale('log')
        ax.set_ylabel('$Stimulus\/\ intensity\/\ g_{max}$', fontsize=15)
        ax.set_ylim(min(y), max(y))
        ax.set_xlim(min(x), max(x))
        ax.set_xlabel('$Velocity\/\ (mm/s)$', fontsize=15)
        cbar = fig.colorbar(cax, ticks=[0, 0.1, 0.2, 0.3, 0.4, 0.5])
        cbar.set_label('$DSI$', fontsize=15)
        plt.tight_layout()
        plt.savefig(plotPath + '/heatmap-' + thevar + str(sac) + '.png', dpi=300)


class Cajero:
  def __init__(self,referencia,monto,interes,destinatario,fecha):
    self.referencia = referencia
    self.monto = monto
    self.interes = interes
    self.destinatario = destinatario
    self.fecha = fecha
  def retirar(self):
    print("retirando")
  def depositar(self):
    print("depositando")
class Banco (Cajero):
  horario  = ""
  iva = ""
  unidad_Monetaria = ""
  sucursal = ""
  def pagar_servicio(self):
    print("pagando")
  def transferencia(self):
    print("transfiriendo")
  def __str__(self):
        return """\
Referencia\t{}
IVA\t\t\t{}
        """.format(self.referencia,self.iva)
cA = Banco("151515151",1500,.16,"yael","hoy")
cA.horario = "1:25"
cA.iva = .16
cA.unidad_Monetaria = "MXN"
cA.sucursal = "Banorte"
print("Metodos")
cA.retirar()
cA.depositar()
cA.pagar_servicio()
cA.transferencia()
print("Atributos")
print(cA)


from math import fabs

n = int(input("Введите размер пятнашек(3 или 4): "))
pole = [[] for i in range(n)]
pole_win = [[] for k in range(n)]
if n == 3:
    pole_numb = 9
    empty_value = [2, 2]
else:
    pole_numb = 16
    empty_value = [3, 3]

def init():
    active_line = 0
    for k in reversed(range(1, pole_numb)):
        pole[active_line].append(k)
        active_line += 1
        if active_line == n:
            active_line = 0
    pole[n - 1].append('_')
    active_line = 0
    for k in range(1, pole_numb):
        pole_win[active_line].append(k)
        active_line += 1
        if active_line == n:
            active_line = 0
    pole_win[n - 1].append('_')

def draw():
    if n == 3:
        for i in range(n):
            print('', end="")
            for k in range(n):
                print("", pole[k][i], "", end="")
            print("")
    else:
        for i in range(n):
            print('', end="")
            for k in range(n):
                print(" ", pole[k][i], "", end="")
            print('')

def win():
    for i in range(n):
        if pole[i] == pole_win[i]:
            continue
        else:
            return 0
    print("You won!You are a MASTERPIECE of human thought!")
    return 1

def move(step):
    print('')
    for i in range(n):
        if step in pole[i]:
            tile_index = [i, pole[i].index(step)]
            for i in range(2):
                if fabs(tile_index[i] - empty_value[i]) > 1:
                    return print("That`s wrong")
            a = tile_index[0]
            b = tile_index[1]
            c = empty_value[0]
            d = empty_value[1]
            if tile_index[0] == empty_value[0] - 1 and tile_index[1] == empty_value[1]:
                pole[a][b], pole[c][d] = pole[c][d], pole[a][b]
                empty_value[0] = a
                empty_value[1] = b
                return print("That`s good")
            elif tile_index[0] == empty_value[0] + 1 and tile_index[1] == empty_value[1]:
                pole[a][b], pole[c][d] = pole[c][d], pole[a][b]
                empty_value[0] = a
                empty_value[1] = b
                return print("Well done")
            elif tile_index[0] == empty_value[0] and tile_index[1] == empty_value[1] + 1:
                pole[a][b], pole[c][d] = pole[c][d], pole[a][b]
                empty_value[0] = a
                empty_value[1] = b
                return print("Nice job")
            elif tile_index[0] == empty_value[0] and tile_index[1] == empty_value[1] - 1:
                pole[a][b], pole[c][d] = pole[c][d], pole[a][b]
                empty_value[0] = a
                empty_value[1] = b
                return print("OMG")
            else:
                return print("Нельзя так")
    return print("Нет такой ячейки")

init()
draw()
won = win()

while won == 0:
    step = int(input("Введите номер ячейки для шага: "))
    move(step)
    draw()
    won = win()


#!/usr/bin/python3
"""
    This module contains test cases for State
"""
import unittest
from models.state import State
from models.base_model import BaseModel
import pep8
from datetime import datetime

class TestState(unittest.TestCase):
    """" Test cases class of State """

    def test_pep8_state(self):
        """pep8 test.
        Makes sure the Python code is up to the pep8 standard.
        """
        syntax = pep8.StyleGuide(quit=True)
        check = syntax.check_files(['models/state.py'])
        self.assertEqual(
            check.total_errors, 0,
            "Found code style errors (and warnings)."
        )

    def test_stateattr(self):
        new_state = State()
        self.assertTrue(hasattr(new_state, "id"))
        self.assertTrue(hasattr(new_state, "created_at"))
        self.assertTrue(hasattr(new_state, "updated_at"))
        self.assertTrue(hasattr(new_state, "name"))

    def test_state(self):
        new_state = State()
        self.assertIs(type(new_state.id), str)
        self.assertIs(type(new_state.created_at), datetime)
        self.assertIs(type(new_state.updated_at), datetime)
        self.assertIs(type(new_state.name), str)

    def test_inherit_State(self):
        new_inherit = State()
        self.assertIsInstance(new_inherit, BaseModel)

    def test_stateisempty(self):
        new_state = State()
        self.assertEqual(new_state.name, "")


n = int(input())
res = []
for i in range(1,n):
    log = True
    res2 = n - i
    str1 = str(i)
    str2 = str(res2)
    for j in range(len(str2)):
        if(str2[j:j+1]=="0"):
            log = False
    for j in range(len(str1)):
        if(str1[j:j+1]=="0"):
            log = False
    if(log==True):
        res = [i,res2]
        break
print (res)


from tkinter import *
import tkinter.messagebox as msgbox
import tkinter.ttk as ttk

root = Tk()
root.geometry("500x300")

def new_project():
    print("Tool Command")

# Menus

menu = Menu(root)
root.config(menu=menu)

file_submenu = Menu(menu, tearoff=0)
menu.add_cascade(label="File", menu=file_submenu)

file_submenu.add_command(label="New Project...", command=new_project)
file_submenu.add_command(label="New...", command=lambda: print("Lambda test"))
file_submenu.add_separator()
file_submenu.add_command(label="Exit", command=lambda: exit(0))

edit_submenu = Menu(menu, tearoff=0)
menu.add_cascade(label="Edit", menu=edit_submenu)

edit_submenu.add_command(label="Undo Typing", command=lambda: print("Undone"))

# Toolbar

toolbar = Frame(root, bg="blue")

save_button = Button(toolbar, text="Save", command=lambda: print("Save Button"))
save_button.pack(side=LEFT, padx=2, pady=2)

open_button = Button(toolbar, text="Open", command=lambda: print("Open Button"))
open_button.pack(side=LEFT, padx=2, pady=2)

toolbar.pack(side=TOP, fill=X)

# Status Bar

status = Label(root, text="Running...", bd=1, relief=SUNKEN, anchor=W)  # bd: border
status.pack(side=BOTTOM, fill=X)

# List Box

list1 = Listbox(root)
list1.insert(0, "A")
list1.insert(1, "B")
list1.insert(2, "C")
list1.pack()

# Combo Box

combo = ttk.Combobox(root)
combo['values'] = ("A", "B", "C")
combo.pack()

# # Message Box
#
#
# msgbox.showinfo("Info", "Hello, this is a popup window")
#
# answer = msgbox.askquestion("Question 1", "Do you like cats?")
# print(answer)
#
# msgbox.showerror("ERROR", "SUPER ERROR!!!")

##########################################
# def left_click(event):
#     print("Left")
#
#
# def mid_click(event):
#     print("Mid")
#
#
# def right_click(event):
#     print("Right")
#
#
# frame = Frame(root, width=300, height=300)
# frame.bind("<Button-1>", left_click)
# frame.bind("<Button-2>", mid_click)
# frame.bind("<Button-3>", right_click)
#
# frame.pack()

###############################
# label_1 = Label(root, text="Name: ")
# label_2 = Label(root, text="Password: ")
# entry_1 = Entry(root)
# entry_2 = Entry(root)
#
# label_1.grid(row=0, sticky=E)  # N, S, E, W. North, South, East, West
# label_2.grid(row=1, sticky=E)
# entry_1.grid(row=0, column=1)
# entry_2.grid(row=1, column=1)
#
# checkbox1 = Checkbutton(root, text="Keep me logged in")
# checkbox1.grid(columnspan=2)
#
#
# # def print_login_data(event):
# #     print("Hello")
#
# def print_login_data():
#     print("Hello")
#
# button1 = Button(root, text="Login", command=print_login_data)
#
# # button1 = Button(root, text="Login")
# # button1.bind("<Button-1>", print_login_data)
#
# button1.grid(column=1, sticky=E)

########################################
# one = Label(root, text="One", bg="red", fg="white")
# one.pack()
#
# two = Label(root, text="Two", bg="green", fg="white")
# two.pack(fill=X)
#
# three = Label(root, text="Two", bg="blue", fg="white")
# three.pack(side=LEFT, fill=Y)

######################################
# top_frame = Frame(root)
# top_frame.pack()
#
# bot_frame = Frame(root)
# bot_frame.pack(side=BOTTOM)
#
# button1 = Button(top_frame, text="Button 1", fg="red")
# button2 = Button(top_frame, text="Button 2", fg="blue")
# button3 = Button(top_frame, text="Button 3", fg="green")
# button4 = Button(top_frame, text="Button 4", fg="yellow")
#
# button5 = Button(bot_frame, text="Button 5", fg="purple")
#
# button1.pack(side=LEFT)
# button2.pack(side=LEFT)
# button3.pack(side=LEFT)
# button4.pack(side=LEFT)
# button5.pack()
###############################################

root.mainloop()


# -------------------------------------------------------------------------
# Copyright 2018, IBM Corp.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# -------------------------------------------------------------------------

from distutils.core import setup, Extension
import sysconfig
import platform
import inspect
import sys
import os

def libname(name) :
   VERSION = "%d_%d" % (sys.version_info[0],sys.version_info[1])
   return "%s_v%s" % (name, VERSION)

def libso(name) :
   # Name mangle the library dependency (see https://www.python.org/dev/peps/pep-3149/)
   EXT= sysconfig.get_config_var('EXT_SUFFIX')
   if (EXT is None) : EXT = ".so"
   return "%s%s" % (libname(name), EXT)

# Define all directories
SYSTEM        = platform.system() + "_" + platform.uname()[-2]
WORKDIR       = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))
OCEAN_ROOT    = os.path.normpath(os.path.join(WORKDIR, '..', '..', '..', '..'))
OCEAN_LIB     = os.path.normpath(os.path.join(OCEAN_ROOT,'lib',SYSTEM))
OCEAN_INCLUDE = os.path.normpath(os.path.join(OCEAN_ROOT,'include'))
PY_OCEAN_ROOT = os.path.normpath(os.path.join(WORKDIR, '..','..'))
PY_OCEAN_LIB  = os.path.normpath(os.path.join(PY_OCEAN_ROOT, 'lib',SYSTEM))

if (platform.system() == "Darwin") :
   PY_OCEAN_SO = []
else :
   PY_OCEAN_SO = [":%s" % libso("pyOcean_cpu")]

# Name
NAME = libname("pyOceanDummy_itf")

# Configuration
pyOceanDummy_Itf = Extension(NAME,
                             libraries=['ocean',
                                        'ocean_dummy_itf'] + PY_OCEAN_SO,
                             library_dirs = [OCEAN_LIB,
                                             PY_OCEAN_LIB],
                             include_dirs = [OCEAN_INCLUDE,
                                             os.path.join(PY_OCEAN_ROOT,'pyOcean','cpu')],
                             sources=[name for name in os.listdir(WORKDIR) if name.endswith('.c')])

# Compile the extension
if (__name__ == "__main__") :
   setup(name=NAME, version="1.0", ext_modules=[pyOceanDummy_Itf])


#!/usr/bin/python3
# coding: utf-8

class compose:
    """compose(func_1, func_2, ..., func_n, func_direct) -> composed_func
when composed_func is called, func_direct will be called with given arguments
then func_n will be called with former result
at last func_1 will be called with the result of func_2
and the final result will be the result of composed_func"""
    __slots__ = ['funcs']

    def __init__(self, *funcs):
        self.funcs = funcs
        for func in funcs:
            if not callable(func):
                raise TypeError("'%s' object is not callable" %
                                type(func).__name__)

    def __call__(self, *args, **kwargs):
        funcs = reversed(self.funcs)
        result = next(funcs)(*args, **kwargs)
        for func in funcs:
            result = func(result)
        return result

    def __repr__(self):
        return "compose(%s)" % ", ".join([repr(i) for i in self.funcs])

    __str__ = __repr__


from django.db.models import signals
from django.contrib.gis.db import models
from django.contrib.auth.models import User
from localground.account.models.permissions import ObjectAuthority

class UserProfile(models.Model):
    #https://docs.djangoproject.com/en/dev/topics/auth/#creating-users
    user = models.OneToOneField('auth.User') # This field is required.
    email_announcements = models.BooleanField(default=True)
    default_location = models.PointField(null=True, blank=True,
                        help_text='Search map by address, or drag the marker to your home location')
    default_view_authority = models.ForeignKey('account.ObjectAuthority',
                                default=1, verbose_name='Share Preference',
                                help_text='Your default sharing settings for your maps and media') #default to private
    contacts = models.ManyToManyField('auth.User', related_name='%(app_label)s_%(class)s_related',
                                      null=True, blank=True,
                                      verbose_name="Users You're Following")
    objects = models.GeoManager()

    class Meta:
        app_label = "account"

def create_profile_on_insert(sender, instance, created, **kwargs):
    #When a new user is created, also create a profile_object.
    #Works just like a database trigger.
    if created:
        profile = UserProfile()
        profile.email_announcements = True
        profile.default_view_authority = ObjectAuthority.objects.get(id=1)
        profile.user = instance
        profile.save()
signals.post_save.connect(create_profile_on_insert, sender=User)


import varstarscan as vss

from os import listdir
from random import choice
import numpy as np

def read_random_asas_test(minlen=20):
    while 1:
        #Pick a random file to get data from it.
        filename= choice(listdir("data"))
        print("testing: ",filename)
        fil= open("data/"+filename)
        reading= False
        datasets= []
        for lin in fil:
            if not reading:
                if lin[:9]=="#     HJD":
                    reading= True
                    datasets.append([])
            else:
                if lin[0]=="#":
                    reading=False
                else:
                    valid_line= list(filter(lambda x: x!='',lin.strip().split(' ')))
                    tim= float(valid_line[0])
                    lum= float(valid_line[1])
                    grade= valid_line[11]
                    if lum<=29.9 and (grade=='A' or grade=='B'):
                        datasets[-1].append((tim,lum))
        fil.close()
        print([len(x) for x in datasets])
        if len(datasets)>0:
            final= choice(datasets)
            if len(final)>=minlen:
                break
    print(filename)
    return final

def main():
    # Pick random filename
    filename= choice(listdir("data"))
    data= vss.get_file_data("data/"+filename)
    print(data)
    vss.save_to_fits(data,"fits/"+filename)

    # Open another to test
    data= read_random_asas_test()
    fitting= vss.fit(data)
    vss.plot(data)

if __name__=="__main__":
    main()


class Question:
    def __init__(self,question):
        self.__question=question
        self.__answer=0
        self.__yes=0
        self.__no=0
    def getAnswer(self,answer):
        if answer.lower()=='yes' or answer.lower()=='no':
            self.__answer=answer
        else:
            answer=input('please insert your answer again:')
            return self.getAnswer(answer)

    def returnQuestion(self):
        return self.__question
    def returAnswer(self):
        return self.__answer
    def increseStatistic(self,response):
        if response=='yes':
            self.__yes+=1
        elif response=='no':
            self.__no+=1
    def returnStatistic(self,response):
        if response=='yes':
            return  self.__yes
        elif response=='no':
            return  self.__no

pizza=Question('Did you like pizza?')
games=Question('Did you like games?')
movies=Question('Do you like movies?')
sport=Question('Do you enjoy doing sport?')
prolan=Question('Do you love Python?')

questionList=[pizza,games,movies,sport,prolan]


class Settings():
	"""A class to stroe all settings for Alien Invasion."""

	def __init__(self):
		"""Initializing the game's settings."""
		# Screen Settings
		self.screen_width = 1200
		self.screen_height = 700
		self.bg_color = (230, 230, 230)

		#Ship settings
		self.ship_speed_factor = 1.5

		#Bullet Settings
		self.bullet_speed_factor = 1
		self.bullet_width = 3
		self.bullet_height = 15
		self.bullet_color = (60, 60, 60)
		self.bullets_allowed = 10


from multiprocessing import Process
from multiprocessing import Event
import time

# 生成事件对象
e = Event()

def wait_event():
    print("wait for event setting")
    e.wait()
    print("wait_for_event01", e.is_set())

def wait_event_timeout():
    print("wait for event setting or timeout")
    e.wait(2)
    print("wait_for_event02", e.is_set())

p1 = Process(name='block', target=wait_event)
p1.start()

p2 = Process(name='non-block', target=wait_event_timeout)
p2.start()

print("main:setting the event")
time.sleep(3)
e.set()
print("event is set")


from pyspark import SparkContext
from pyspark.streaming import StreamingContext
import json
import requests

def sentimentValue(sentiment):
  if (sentiment == "positive"):
    return(1)
  elif (sentiment == "negative"):
    return(-1)
  else:
    return(0)

def getLineSentiment(line):
  words = line.lower().split()
  tsent = 0
  for word in words:
    wsent = 0
    try:
      wsent = sentimentValue(dwsent[word.replace("#","").replace("@","")].encode('ascii'))
    except:
      wsent = 0
    finally:
      tsent = tsent + wsent
  if tsent > 0:
    return("positive")
  elif tsent < 0:
    return("negative")
  else:
    return("neutral")

def getShow(line):
  words = line.lower().split()
  tsent = 0
  for word in words:
    try:
      show = shows[word.replace("#","").replace("@","").replace(":","")].encode('ascii')
      return(show)
    except:
      None

def getText(line):
  j = json.loads(line);
  return((j["created_at"], j["id"], j["text"]))

def postToSolr(t, rdd):
  solr_url = "http://auisbigdatabox.cloudapp.net:8983/solr/project/update?commitWithin=5000"
  d = json.dumps(rdd.map(lambda x: x).collect())
  print(d)
  h = {'content-type': 'application/json'}
  r = requests.post(solr_url, data=d, headers=h)
  print(r.status_code)
  #r2 = requests.get("http://auisbigdatabox.cloudapp.net:8983/solr/admin/cores?action=RELOAD&core=project_shard1_replica1")
  #print(r2.status_code)

sc = SparkContext()

dwsent = dict(sc.textFile("/data/dictionary.tsv").map(lambda line: line.split("\t")) \
  .map(lambda line: (line[2], line[5])).collect())
shows = dict(sc.textFile("/data/shows.txt").map(lambda line: (line.lower().encode('ascii'), line)).collect())

ssc = StreamingContext(sc, 1)
ds = ssc.textFileStream("/project/rawdata")
ods = ds.window(30, 30).map(lambda line: getText(line)) \
  .map(lambda line: {"created_dt": line[0], "show_s": getShow(line[2]), "sentiment_s": getLineSentiment(line[2]), "text_t": line[2]})
ods.foreachRDD(postToSolr)

ssc.start()
ssc.awaitTermination()