# Copyright (c) 2014 Dark Secret Software Inc.
#
# 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.

import fnmatch
import os
import os.path

import notification_utils

import archive
import disk_storage

class NoMoreFiles(Exception):
    pass

class NoValidFile(Exception):
    pass

class RollManager(object):
    def __init__(self, filename_template, directory=".",
                 archive_class=None, archive_callback=None):
        self.filename_template = filename_template
        self.directory = directory
        self.active_archive = None
        self.archive_class = archive_class
        self.active_filename = None
        self.archive_callback = archive_callback

    def _roll_archive(self):
        self.close()
        self.get_active_archive()

    def close(self):
        if self.active_archive:
            self.active_archive.close()
            if self.archive_callback:
                self.archive_callback.on_close(self.active_filename)
            self.active_archive = None
            self.active_filename = None

class ReadingRollManager(RollManager):
    def __init__(self, filename_template, directory=".",
                 archive_class = archive.ArchiveReader,
                 archive_callback=None):
        super(ReadingRollManager, self).__init__(
                                            filename_template,
                                            directory=directory,
                                            archive_callback=archive_callback,
                                            archive_class=archive_class)
        self.files_to_read = self._get_matching_files(directory,
                                                      filename_template)

    def _get_matching_files(self, directory, filename_template):
        files = [os.path.join(directory, f)
                    for f in os.listdir(self.directory)
                        if os.path.isfile(os.path.join(directory, f))]
        return sorted(fnmatch.filter(files, filename_template))

    def read(self):
        # (metadata, payload)
        for x in range(3):  # 3 attempts to file a valid file.
            a = self.get_active_archive()
            try:
                return a.read()
            except disk_storage.EndOfFile:
                self._roll_archive()
        raise NoValidFile("Unable to find a valid file after 3 attempts")

    def get_active_archive(self):
        if not self.active_archive:
            if not self.files_to_read:
                raise NoMoreFiles()
            self.active_filename = self.files_to_read.pop(0)
            self.active_archive = self.archive_class(self.active_filename)
            if self.archive_callback:
                self.archive_callback.on_open(self.active_filename)
        return self.active_archive

class WritingRollManager(RollManager):
    def __init__(self, filename_template, roll_checker, directory=".",
                 archive_class=archive.ArchiveWriter,
                 archive_callback=None):
        super(WritingRollManager, self).__init__(
                                            filename_template,
                                            directory=directory,
                                            archive_callback=archive_callback,
                                            archive_class=archive_class)
        self.roll_checker = roll_checker

    def write(self, metadata, payload):
        """metadata is string:string dict.
           payload must be encoded as string.
        """
        a = self.get_active_archive()
        a.write(metadata, payload)
        if self._should_roll_archive():
            self._roll_archive()

    def _make_filename(self):
        f = notification_utils.now().strftime(self.filename_template)
        f = f.replace(" ", "_")
        f = f.replace("/", "_")
        f = f.replace(":", "_")
        return os.path.join(self.directory, f)

    def get_active_archive(self):
        if not self.active_archive:
            self.active_filename = self._make_filename()
            self.active_archive = self.archive_class(self.active_filename)
            if self.archive_callback:
                self.archive_callback.on_open(self.active_filename)
            self.roll_checker.start(self.active_archive)
        return self.active_archive

    def _should_roll_archive(self):
        return self.roll_checker.check(self.active_archive)


import requests

string = '_diffrn_detector.pdbx_collection_date'

def cif_downloader(ID):
    full_url = "https://files.rcsb.org/download/"
    full_url += ID + ".cif"
    response = requests.get(full_url)
    open(f"{ID}.cif", 'wb').write(response.content)

def lines_that_contain(string, fp):
    return [line for line in fp if string in line]

def collection_date(cif_file):
    result = lines_that_contain(string, cif_file)
    date = str(result).split('20')[1]
    return(date)

PDB_ID = '3FY7'
PDB_file = open(PDB_ID + ".cif")

cif_downloader(PDB_ID)
print(collection_date(PDB_file))


from django.shortcuts import render,redirect,get_object_or_404
from rest_framework.views import APIView
from rest_framework.response import Response
from  rest_framework import status
from rest_framework.renderers import JSONRenderer
from .models import Appliance
from .serializers import ApplianceSerializer
from django.http import HttpResponseRedirect,HttpResponse
import os
# Create your views here.

### This is for the app!

###All the Instanes of Appliance model are having the name in this pattern ([A-Z][a-z]+)
###I have used only get methods to handle the request from the app

#This class handles the part where application asks for state of appliance
class ApplianceList(APIView):
    def get(self, request,appliance_name):
        if appliance_name == "All":
            Appli = Appliance.objects.all()
            serializer = ApplianceSerializer(Appli,many=True)
        else:
            Appli = Appliance.objects.get(Name=appliance_name)
            serializer = ApplianceSerializer(Appli)
        return Response(serializer.data)

###This will handle turning on or off of the appliance
def StateHandler(request,name_of_appliance,command):
    subject = Appliance.objects.get(Name=name_of_appliance)
    if (command == 'turnon'):
        subject.state = 'ON'
        subject.turnon()
        subject.save()
        return HttpResponse(str(name_of_appliance) + " has been turned "+"on")
    if (command == 'turnoff'):
        subject.state = 'OFF'
        subject.turnoff()
        subject.save()
        return HttpResponse(str(name_of_appliance) + " has been turned " + "off")

###This is the web part!!!!It has nothing to do with android app
###But we will be taking inspiration from this
def home(request):
    queryset = Appliance.objects.all()
    context = {
        'Appliances' : queryset,
    }
    if request.method == "POST":
        subject = Appliance.objects.get(Name = request.POST.get('appliance'))
        desired_state = request.POST.get('state')
        if (desired_state == 'on'):
            subject.state = 'ON'
            subject.turnon()
            subject.save()

        if (desired_state == None):
            subject.state = 'OFF'
            subject.turnoff()
            subject.save()
        redirect("/")
    return render(request, 'Homepage.html',context)


#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
This is a skeleton file that can serve as a starting point for a Python
console script. To run this script uncomment the following line in the
entry_points section in setup.py:

    [console_scripts]
    fibonacci = climath.skeleton:run

Then run `python setup.py install` which will install the command `fibonacci`
inside your current environment.
Besides console scripts, the header (i.e. until _logger...) of this file can
also be used as template for Python modules.

Note: This skeleton file can be safely removed if not needed!
"""

import argparse
import sys
import logging
import functools

__author__ = "Branden Kappes"
__copyright__ = "Branden Kappes"
__license__ = "mit"

_logger = logging.getLogger(__name__)

def _generate_parse_args(description):
    def parse_args(args):
        """Parse command line parameters

        Args:
        :param args: Argument(s) to process. If args is a list of strings, then
            this is the command line parser for the main function. Otherwise--
            that is, args is a subparser instance--this adds subcommand
            arguments to this function call.
        :type args: list of str or subparser object (see above).

        Returns:
        :return: `argparse.Namespace` if `args` is a list of strings, otherwise
            the subparser object passed as `args` is modified/augmented in place
        :rtype: argparse.Namespace or None (see above).
        """
        # If called as a main function, this processes command line arguments
        # as main. If this is called as part of an action
        if isinstance(args, list):
            parser = argparse.ArgumentParser(description=description)
        else:
            parser = args
        # add required parameters for this application
        parser.add_argument("operands",
            nargs='+',
            type=float,
            help="List of operands.")
        # add options for this application
        parser.add_argument(
            '-v',
            '--verbose',
            dest="loglevel",
            help="set loglevel to INFO",
            action='store_const',
            const=logging.INFO)
        parser.add_argument(
            '-vv',
            '--very-verbose',
            dest="loglevel",
            help="set loglevel to DEBUG",
            action='store_const',
            const=logging.DEBUG)
        if isinstance(args, list):
            return parser.parse_args(args)
    return parse_args

def setup_logging(loglevel):
    """Setup basic logging

    Args:
      loglevel (int): minimum loglevel for emitting messages
    """
    logformat = "[%(asctime)s] %(levelname)s:%(name)s:%(message)s"
    logging.basicConfig(level=loglevel, stream=sys.stdout,
                        format=logformat, datefmt="%Y-%m-%d %H:%M:%S")

def _generate_main(reduction):
    name = reduction.__name__
    def main(args):
        """Main entry point allowing external calls

        Args:
          args ([str]): command line parameter list
        """
        if isinstance(args, list):
            args = parse_args(args)
        setup_logging(args.loglevel)
        _logger.debug(f"Starting ({name}) operation...")
        answer = reduction(args.operands)
        print(f"{answer}")
        _logger.info(f"End climath ({name}).")

    return main


"""Container for SSE step."""
import graph_tool
import copy
import functools
import json

import pyllco

from ara.graph import (ABBType, Graph, CFGView, CFType, CallPath,
                       SyscallCategory, InstanceGraph)
from .step import Step
from .option import Option, String, Bool
from ara.os.freertos import Task
from ara.util import VarianceDict

from collections import defaultdict
from itertools import chain
from graph_tool.topology import (dominator_tree, label_out_component,
                                 all_paths, all_circuits)

class State:
    def __init__(self, cfg=None, callgraph=None, next_abbs=None):
        self.cfg = cfg
        self.callgraph = callgraph
        if not next_abbs:
            next_abbs = []
        self.next_abbs = next_abbs

        self.instances = InstanceGraph()
        self.call_path = None # call node within the call graph
        self.branch = False # is this state coming from a branch
        self.usually_taken = False # is this state coming from a branch where
                                   # all other branches ends in an endless loop
        self.loop = False # is this state coming from a loop
        self.recursive = False #is this state executing in a recursive function
        self.running = None # what instance (Task or ISR) is currently running

    def __repr__(self):
        ret = f"State(B:{int(self.branch)},L:{int(self.loop)},"
        ret += f"R:{int(self.recursive)},U:{int(self.usually_taken)}, "
        abbs = [self.cfg.vp.name[abb] for abb in self.next_abbs]
        ret += ', '.join(abbs)
        ret += ", CallPath: " + self.call_path.print(call_site=True)
        return ret + ')'

    def copy(self):
        scopy = State()
        scopy.instances = self.instances.copy()
        scopy.call_path = copy.copy(self.call_path)
        for key, value in self.__dict__.items():
            if key in ['instances', 'call_path']:
                continue
            setattr(scopy, key, value)
        return scopy

class FlowAnalysis(Step):
    """Base class for all flow analyses.

    Apply the base SSE state search to the CFG. Can be specialized with some
    interface functions.
    """

    entry_point = Option(name="entry_point",
                         help="system entry point",
                         ty=String())
    stats = Option(name="stats",
                   help="Output stats file (Respects dump_prefix).",
                   ty=Bool())

    def new_vertex(self, sstg, state):
        vertex = sstg.add_vertex()
        sstg.vp.state[vertex] = state
        return vertex

    def _get_os_specific_deps(self):
        if self._graph.os is None:
            return ['SysFuncts']
        return self._graph.os.get_special_steps()

    def _require_instances(self):
        if self._graph.os is None:
            return ['SysFuncts']
        deps = self._graph.os.get_special_steps()
        if self._graph.os.has_dynamic_instances():
            deps.append('SIA')
        return deps

    def _system_semantic(self, state):
        new_states = self._execute(state)
        self._schedule(new_states)
        return new_states

    def run(self):
        entry_label = self.entry_point.get()
        if not entry_label:
            self._fail("Entry point must be given.")
        self._log.info(f"Analyzing entry point: '{entry_label}'")

        self._step_data = self._get_step_data(set)

        self._stats = { "name": entry_label }

        self._cfg = self._graph.cfg
        self._icfg = CFGView(self._graph.cfg, efilt=self._graph.cfg.ep.type.fa == CFType.icf)
        self._lcfg = CFGView(self._graph.cfg, efilt=self._graph.cfg.ep.type.fa == CFType.lcf)

        self._entry_func = entry_label

        self._init_analysis()

        sstg = graph_tool.Graph()
        sstg.vertex_properties["state"] = sstg.new_vp("object")

        state_vertex = self.new_vertex(sstg, self._get_initial_state())

        stack = [state_vertex]

        counter = 0
        while stack:
            self._log.debug(f"Round {counter:3d}, "
                            f"Stack with {len(stack)} state(s): "
                            f"{[sstg.vp.state[v] for v in stack]}")
            state_vertex = stack.pop()
            state = sstg.vp.state[state_vertex]
            for n in self._system_semantic(state):
                new_state = self.new_vertex(sstg, n)
                sstg.add_edge(state_vertex, new_state)
                stack.append(new_state)
            counter += 1
        self._log.info(f"Analysis needed {counter} iterations.")

        self._stats["iterations"] = counter

        self._finish(sstg)

        stats = self.stats.get()
        if stats:
            stat_file = f'.{entry_label}.statistics.json'
            stat_file = self.dump_prefix.get() + stat_file

            with open(stat_file, 'w') as f:
                json.dump(self._stats, f, indent=4)

class FlatAnalysis(FlowAnalysis):
    """Analysis that run one time over the control flow reachable from the
    entry point.

    This analysis does not respect loops.
    """
    def get_single_dependencies(self):
        return ["RecursiveFunctions"]

    def _init_analysis(self):
        self._call_graph = self._graph.callgraph
        self._cond_func = {}
        self._ut_func = {}
        self._loop_func = {}
        self._step_data.add(self._entry_func)

        self._visited = defaultdict(lambda: defaultdict(lambda: False))
        self._max_call_depth = 0

    def _get_initial_state(self):
        cfg = self._graph.cfg
        callgraph = self._graph.callgraph

        # find main
        entry_func = cfg.get_function_by_name(self._entry_func)
        entry_abb = cfg.get_entry_abb(entry_func)

        entry = State(cfg=cfg,
                      callgraph=callgraph,
                      next_abbs=[entry_abb])

        self._graph.os.init(entry)

        entry.call_path = CallPath()
        entry.scheduler_on = self._is_chained_analysis(self._entry_func)
        entry.recursive = callgraph.vp.recursive[
            callgraph.vertex(cfg.vp.call_graph_link[entry_func])
        ]
        instance = self._find_running_instance(self._entry_func)
        entry.running = instance
        if instance:
            entry.branch = self._graph.instances.vp.branch[instance]
            entry.loop = self._graph.instances.vp.loop[instance]

        return entry

    def _iterate_tasks(self):
        """Return a generator over all tasks in self._graph.instances."""
        if self._graph.instances is None:
            return
        for v in self._graph.instances.vertices():
            os_obj = self._graph.instances.vp.obj[v]
            if isinstance(os_obj, Task) and os_obj.is_regular:
                yield os_obj, v

    def _get_task_function(self, task):
        """Return the function which defines a task."""
        assert task.entry_abb is not None, "Not a regular Task."
        entry = self._graph.cfg.vertex(task.entry_abb)
        return self._graph.cfg.get_function(entry)

    def _find_running_instance(self, entry_func):
        for task, v in self._iterate_tasks():
            func = self._get_task_function(task)
            if self._graph.cfg.vp.name[func] == entry_func:
                return v
        return None

    def _is_chained_analysis(self, entry_func):
        return self._find_running_instance(entry_func) is not None

    def _init_execution(self, state):
        pass

    def _init_fake_state(self, state, abb):
        pass

    def _evaluate_fake_state(self, state, abb):
        pass

    def _handle_call(self, old_state, new_state, abb):
        pass

    def _is_bad_call_target(self, abb):
        return False

    def _get_categories(self):
        return SyscallCategory.every

    def _get_call_node(self, call_path, abb):
        """Return the call node for the given abb, respecting the call_path."""
        edge = self._call_graph.get_edge_for_callsite(abb)
        if edge is None:
            self._fail(f"Cannot find call path for ABB {abb_name}.")
        new_call_path = copy.copy(call_path)
        new_call_path.add_call_site(self._call_graph, edge)
        return new_call_path

    def _execute(self, state):
        new_states = []
        self._init_execution(state)
        for abb in state.next_abbs:
            # don't handle already visited vertices
            if self._visited[state.call_path][abb]:
                continue
            self._visited[state.call_path][abb] = True
            self._log.debug(f"Handle state {state}")
            call_depth = len(state.call_path)
            if self._max_call_depth < call_depth:
                self._max_call_depth = call_depth

            # syscall handling
            if self._icfg.vp.type[abb] == ABBType.syscall:
                name = self._icfg.vp.name[abb]
                syscall_name = self._cfg.get_syscall_name(abb)
                self._log.debug(f"Handle syscall: {name} ({syscall_name})")
                fake_state = state.copy()
                self._init_fake_state(fake_state, abb)
                assert self._graph.os is not None
                new_state = self._graph.os.interpret(
                    self._graph.cfg, abb, fake_state,
                    categories=self._get_categories()
                )
                self._evaluate_fake_state(new_state, abb)
                new_states.append(new_state)

            # call handling
            elif self._icfg.vp.type[abb] == ABBType.call:
                func = self._cfg.vp.name[self._cfg.get_function(abb)]
                self._log.debug(f"Handle call: {self._icfg.vp.name[abb]} in {func}")
                handled = False
                for n in self._icfg.vertex(abb).out_neighbors():
                    if self._is_bad_call_target(n):
                        continue
                    new_call_path = self._get_call_node(state.call_path, abb)
                    if new_call_path.is_recursive():
                        self._log.debug(f"Reentry of recursive function. Callpath {new_call_path}")
                        continue
                    new_state = state.copy()
                    new_state.next_abbs = [n]
                    new_state.call_path = new_call_path
                    func = new_state.cfg.get_function(new_state.cfg.vertex(n))
                    new_state.recursive = new_state.callgraph.vp.recursive[
                        new_state.callgraph.vertex(
                            new_state.cfg.vp.call_graph_link[func]
                        )
                    ]
                    self._handle_call(state, new_state, abb)
                    new_states.append(new_state)
                    handled = True
                # if only recursive functions are found, handle the call like a
                # normal computation block
                if not handled:
                    for n in self._lcfg.vertex(abb).out_neighbors():
                        new_state = state.copy()
                        new_state.next_abbs = [n]
                        new_states.append(new_state)

            # exit handling
            elif (self._icfg.vp.is_exit[abb] and
                  self._icfg.vertex(abb).out_degree() > 0):
                self._log.debug(f"Handle exit: {self._icfg.vp.name[abb]}")
                new_state = state.copy()
                callsite = new_state.call_path[-1]
                call = new_state.callgraph.ep.callsite[callsite]
                neighbors = self._lcfg.vertex(call).out_neighbors()
                next_node = next(neighbors)
                func = new_state.cfg.get_function(
                    new_state.cfg.vertex(next_node)
                )
                new_state.recursive = new_state.callgraph.vp.recursive[
                    new_state.callgraph.vertex(
                        new_state.cfg.vp.call_graph_link[func]
                    )
                ]
                new_state.next_abbs = [next_node]
                new_state.call_path.pop_back()
                new_states.append(new_state)

            # computation block handling
            else:
                self._log.debug(f"Handle computation: {self._icfg.vp.name[abb]}")
                for n in self._icfg.vertex(abb).out_neighbors():
                    new_state = state.copy()
                    new_state.next_abbs = [n]
                    new_states.append(new_state)

        return new_states

    def _schedule(self, state):
        # we do simply not care
        return

    def _finish(self, sstg):
        self._log.info(f"Maximal call depth: {self._max_call_depth}")
        self._stats["maximal_call_depth"] = self._max_call_depth
        if self.dump.get():
            dot_file = f'{self.dump_prefix.get()}.{self._entry_func}.dot'
            self._step_manager.chain_step({"name": "Printer",
                                           "dot": dot_file,
                                           "graph_name": 'Instances',
                                           "subgraph": 'instances'})

class SIA(FlatAnalysis):
    """Static Instance Analysis: Find all application instances."""

    def _get_entry_point_dep(self, name):
        return {"name": name, "entry_point": self.entry_point.get()}

    def get_single_dependencies(self):
        deps = super().get_single_dependencies()
        deps += self._get_os_specific_deps()
        deps += list(map(self._get_entry_point_dep,
                         ["Syscall", "ValueAnalysis"]))
        return deps

    def _init_analysis(self):
        super()._init_analysis()
        self._new_entry_points = set()

    def _dominates(self, dom_tree, abb_x, abb_y):
        """Does abb_x dominate abb_y?"""
        while abb_y:
            if abb_x == abb_y:
                return True
            abb_y = dom_tree[abb_y]
        return False

    def _has_path(self, graph, source, target):
        ap = all_paths(graph, graph.vertex(source), graph.vertex(target))
        try:
            next(ap)
            return True
        except StopIteration:
            return False

    @functools.lru_cache(maxsize=32)
    def _get_func_cfg(self, func):
        """Get LCFG of function"""
        abb = self._cfg.get_entry_abb(func)
        comp = label_out_component(self._lcfg, self._lcfg.vertex(abb))
        return CFGView(self._lcfg, vfilt=comp)

    @functools.lru_cache(maxsize=32)
    def _create_dom_tree(self, func, ignore_endless_loops=False):
        """Create a dominator tree of the local control flow for func."""
        func_cfg = self._get_func_cfg(func)
        entry = self._cfg.get_entry_abb(func)

        # prepare LCFG, endless loops are filtered out and replaces by exit
        # blocks
        exit_map = func_cfg.vp.is_exit.copy(full=False)
        keep_edge_map = func_cfg.new_ep("bool", val=True)

        if not ignore_endless_loops:
            loops = CFGView(func_cfg, vfilt=func_cfg.vp.is_exit_loop_head)
            for v in loops.vertices():
                v = func_cfg.vertex(v)
                for e in v.in_edges():
                    if self._has_path(func_cfg, v, e.source()):
                        keep_edge_map[e] = False
                        exit_map[e.source()] = True

        patched_func_cfg = CFGView(func_cfg, efilt=keep_edge_map)

        # dom tree creation
        dom_tree = dominator_tree(patched_func_cfg,
                                  patched_func_cfg.vertex(entry))
        return dom_tree, CFGView(func_cfg, vfilt=exit_map)

    def _is_in_condition(self, abb, ignore_endless_loops=False):
        """Is abb part of a condition?"""
        func = self._graph.cfg.get_function(abb)
        dom_tree, exit_abbs = self._create_dom_tree(func,
                                                    ignore_endless_loops=ignore_endless_loops)
        res = not all([self._dominates(dom_tree, abb, x)
                      for x in exit_abbs.vertices()])
        return res

    def _is_in_loop(self, abb):
        """Is abb part of a loop?"""
        return self._cfg.vp.part_of_loop[abb]

    def _is_usually_taken(self, state, abb):
        in_cond = self._is_in_condition(abb)
        local_ut = (in_cond and not
                    self._is_in_condition(abb, ignore_endless_loops=True))
        extern_ut = self._ut_func.get(state.call_path, False)
        return local_ut or (extern_ut and not in_cond)

    def _init_fake_state(self, state, abb):
        state.branch = (self._cond_func.get(state.call_path, False) or
                        self._is_in_condition(abb))

        state.usually_taken = self._is_usually_taken(state, abb)

        state.loop = (self._loop_func.get(state.call_path, False) or
                      self._is_in_loop(abb))

    def _extract_entry_points(self):
        for task, _ in self._iterate_tasks():
            if (task not in self._new_entry_points):
                func_name = self._graph.cfg.vp.name[self._get_task_function(task)]
                if func_name not in self._step_data:
                    # order is different here, the first chained step will
                    # be the last executed one
                    self._step_manager.chain_step({"name": self.get_name(),
                                                   "entry_point": func_name})
                    self._step_data.add(func_name)
                self._new_entry_points.add(task)

    def _evaluate_fake_state(self, new_state, abb):
        self._graph.instances = new_state.instances
        self._extract_entry_points()

    def _init_execution(self, state):
        if self._graph.instances is not None:
            state.instances = self._graph.instances

    def _handle_call(self, old_state, new_state, abb):
        new_state.branch = (self._cond_func.get(old_state.call_path, False) or
                            self._is_in_condition(abb))
        self._cond_func[new_state.call_path] = new_state.branch

        new_state.usually_taken = self._is_usually_taken(old_state, abb)
        self._ut_func[new_state.call_path] = new_state.usually_taken

        new_state.loop = (self._loop_func.get(old_state.call_path, False) or
                          self._is_in_loop(abb))
        self._loop_func[new_state.call_path] = new_state.loop

    def _get_categories(self):
        return SyscallCategory.create

    def _is_bad_call_target(self, abb):
        cfg = self._graph.cfg
        cg = self._graph.callgraph
        cg_vertex = cg.vertex(cfg.vp.call_graph_link[cfg.get_function(abb)])
        return not cg.vp.syscall_category_create[cg_vertex]

    def _finish(self, sstg):
        super()._finish(sstg)
        self._log.debug(f"_get_func_cfg {self._get_func_cfg.cache_info()}")
        self._log.debug(f"_create_dom_tree {self._create_dom_tree.cache_info()}")

class InteractionAnalysis(FlatAnalysis):
    """Find the flow insensitive interactions between instances."""

    def get_single_dependencies(self):
        return self._require_instances()

    def _init_analysis(self):
        super()._init_analysis()
        self._chain_entry_points()

    def _chain_entry_points(self):
        for task, _ in self._iterate_tasks():
            func_name = self._graph.cfg.vp.name[self._get_task_function(task)]
            if func_name not in self._step_data:
                self._step_manager.chain_step({"name": self.get_name(),
                                               "entry_point": func_name})
                self._step_data.add(func_name)

    def _get_categories(self):
        return SyscallCategory.comm

    def _evaluate_fake_state(self, new_state, abb):
        self._graph.instances = new_state.instances

    def _init_execution(self, state):
        if self._graph.instances is not None:
            state.instances = self._graph.instances


import pygame
from pygame.sprite import Sprite

class ViewElement(Sprite):
    """
    A basic element which is rendered to a view.
    """
    def __init__(self, name=None, rect=None, background=None,
                  parent=None, **kwargs):
        super().__init__()

        # The name of the element
        self.name = name

        # The element's rectangle, relative to the parent
        self.base_rect = rect if rect is not None else pygame.Rect(0,0,0,0)

        # The parent element, if any. If there is one, this will be placed
        # relative to the parent
        self.parent = parent

        # The elements's bounding rectangle, i.e. its actual position on-screen
        # This should always be defined BEFORE you call super().__init__ in any
        # subclasses!
        self.rect = self.get_rect()

        # The image to render
        self.image = pygame.Surface(self.rect.size, pygame.SRCALPHA)

        if background:
            self.image.fill(background)

    def get_rect(self):
        """
        Returns the element's actual rect, adjusted to parent position
        """
        rect = self.base_rect.copy()

        # Offset by parent rectangle
        if self.parent:
            parent_rect = self.parent.get_rect()

            rect.move_ip(parent_rect.x, parent_rect.y)

        return rect

    def update(self):
        """
        Updates the element's position.
        """
        self.rect = self.get_rect()

        super().update()

    def click(self, event):
        """
        Called when the element is clicked.
        """
        pass


import matplotlib.pyplot as plt

input_values = [1, 2, 3, 4, 5]
squares = [1, 4, 9, 16, 25]
plt.plot(input_values, squares, linewidth=5)

#Zdefiniowanie tytułu wykresu i etykiet osi.
plt.title("Kwadraty liczb", fontsize=25)
plt.xlabel("Wartość", fontsize=14)
plt.ylabel("Kwadraty wartości", fontsize=14)

#Zdefiniowanie wielkości etykiet.
plt.tick_params(axis='both', labelsize=14)

plt.show()


DEBUG = True
TEMPLATE_DEBUG = DEBUG

SECRET_KEY = 'fake_secret'

ROOT_URLCONF = 'tests.test_urls'

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': 'irrelevant.db'
    }
}

MIDDLEWARE_CLASSES = []

INSTALLED_APPS = (
    'djproxy',
)

STATIC_ROOT = ''
STATIC_URL = '/'

APPEND_SLASH = False


import numpy as np
import re
import csv
from pprint import pprint
import matplotlib.pyplot as plt
import string
import word2vec
import gensim
from sklearn.pipeline import Pipeline
from sklearn.ensemble import ExtraTreesClassifier
from sklearn.svm import SVC
from tabulate import tabulate
import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd
import numpy as np
from gensim.models.word2vec import Word2Vec
from collections import Counter, defaultdict
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.naive_bayes import BernoulliNB, MultinomialNB
from sklearn.metrics import accuracy_score
from sklearn.cross_validation import cross_val_score
from sklearn.cross_validation import StratifiedShuffleSplit
import zipfile
import json

# build a sklearn-compatible transformer that is initialised with a word -> vector dictionary.
class MeanEmbeddingVectorizer(object):
    def __init__(self, word2vec):
        self.word2vec = word2vec
        # if a text is empty we should return a vector of zeros
        # with the same dimensionality as all the other vectors
        self.dim = len(next(iter (word2vec.values())))

    def fit(self, X, y):
        return self

    def transform(self, X):
        return np.array([
            np.mean([self.word2vec[w] for w in words if w in self.word2vec]
                    or [np.zeros(self.dim)], axis=0)
            for words in X
        ])

class SumEmbeddingVectorizer(object):
    def __init__(self, word2vec):
        self.word2vec = word2vec
        # if a text is empty we should return a vector of zeros
        # with the same dimensionality as all the other vectors
        self.dim = len(next(iter (word2vec.values())))

    def fit(self, X, y):
        return self

    def transform(self, X):
        return np.array([
            np.sum([self.word2vec[w] for w in words if w in self.word2vec]
                    or [np.zeros(self.dim)], axis=0)
            for words in X
        ])

# and a tf-idf version of the same
class TfidfEmbeddingVectorizer(object):
    def __init__(self, word2vec):
        self.word2vec = word2vec
        self.word2weight = None
        self.dim = len(next(iter (word2vec.values())))

    def fit(self, X, y):
        tfidf = TfidfVectorizer(analyzer=lambda x: x, smooth_idf=True)
        tfidf.fit(X)
        # if a word was never seen - it must be at least as infrequent
        # as any of the known words - so the default idf is the max of
        # known idf's
        max_idf = max(tfidf.idf_)
        self.word2weight = defaultdict(
            lambda: max_idf,
            [(w, tfidf.idf_[i]) for w, i in tfidf.vocabulary_.items()])

        return self

    def transform(self, X):
        '''
        return np.array([
                np.mean([self.word2vec[w] * self.word2weight[w]
                         for w in words if w in self.word2vec] or
                        [np.zeros(self.dim)], axis=0)
                for words in X
        '''
        return np.array([
                np.mean([self.word2vec[w]
                         for w in words if w in self.word2vec] or
                        [np.zeros(self.dim)], axis=0)
                for words in X
            ])

class TfidfEmbeddingVectorizerSum(object):
    def __init__(self, word2vec):
        self.word2vec = word2vec
        self.word2weight = None
        self.dim = len(next(iter (word2vec.values())))

    def fit(self, X, y):
        tfidf = TfidfVectorizer(analyzer=lambda x: x, norm = 'l2')
        tfidf.fit(X)
        # if a word was never seen - it must be at least as infrequent
        # as any of the known words - so the default idf is the max of
        # known idf's
        max_idf = max(tfidf.idf_)
        self.word2weight = defaultdict(
            lambda: max_idf,
            [(w, tfidf.idf_[i]) for w, i in tfidf.vocabulary_.items()])

        return self

    def transform(self, X):
        return np.array([
                np.sum([self.word2vec[w]
                         for w in words if w in self.word2vec] or
                        [np.zeros(self.dim)], axis=0)
                for words in X
            ])

############################# STATS FUNCTIONS ##################################
def make_stats(predicitons, ytest):
    #First calculate the accuracy
    acc = accuracy(predicitons, ytest)
    vals = negs_and_pos(predicitons, ytest)
    recall, precision = recall_and_precision(vals[0], vals[1], vals[2], vals[3])
    return acc, recall, precision

def accuracy(predicitons, ytest):
    score = np.zeros_like(predicitons)
    for l in range(len(predicitons)):
        if predicitons[l] == ytest[l]:
            score[l] = 1
    accuracy = np.sum(score)/len(score)
    return accuracy

def negs_and_pos(predictions, ytest):
    tp = 0
    tn = 0
    fn = 0
    fp = 0
    for l in range(len(predictions)):
        if predictions[l] == 1 and ytest[l] == 1:
            tp += 1
        if predictions[l] == 1 and ytest[l] == 0:
            fp += 1
        if predictions[l] == 0 and ytest[l] == 1:
            fn += 1
        if predictions[l] == 0 and ytest[l] == 0:
            tn += 1
    return tp, tn, fn, fp

def recall_and_precision(tp, tn, fn, fp):
    recall = tp / ((tp + fn)+0.0000001)
    precision = tp / ((tp + fp)+0.000001)           #to prevent division by zero problems
    return recall, precision

#load in the stop words
with open('stopwords.txt', 'r') as file:
    stopWords =  file.read().splitlines()
#At first we are gonna leave in the stop words because think they might actually give useful information ie We present

#Load in the identifier setences
with open('diseaseidentifiers.txt', 'r') as file:
    identifiers = file.read().splitlines()

with open('nonIdentifiers.txt', 'r') as file:
    nonId = file.read().splitlines()

#Load in the identifier sentences
X, y = [], []
j = 0
for id in identifiers:
    id = id.lower()                                         #Make everything lower case
    id = re.sub('['+string.punctuation+']', '', id)         #Strip out the punctuation
    words = id.split()
    X.append(words)
    y.append('1')
    j += 1
#Load in the none identifier sentences
i = 0
for id in nonId:
    #while i < j + :        #So get the same number of each type of sentence
    id = id.lower()                                         #Make everything lower case
    id = re.sub('['+string.punctuation+']', '', id)         #Strip out the punctuation
    words = id.split()
    X.append(words)
    y.append('0')
    i += 1

#Convert to numpy arrays to be able to use fancy idexing etc
X = np.array(X)
y = np.array(y)

#Shuffle X and y and split into testing and training sets
shuffler = np.random.permutation(range(X.shape[0]))
X = X[shuffler]
y = y[shuffler]

# Make some predictions
proportion = int(0.9 * X.shape[0])
Xtest = X[proportion:]
Xtrain = X[0:proportion]
ytest = y[proportion:]
ytrain = y[0:proportion]

# train word2vec on all the texts - both training and test set# train
# we're not using test labels, just texts so this is fine
model = Word2Vec(Xtrain, size=100, window=5, min_count=5, workers=2)
w2v = {w: vec for w, vec in zip(model.wv.index2word, model.wv.syn0)}

#So far we have a dictionary mapping words --> 100 dimensional vector
#We can use this to build features. The simplest way to do this is by averaging
#word vectors for all words in a text.

#These are trained only on the training data
#Mean vector
# start with the classics - naive bayes of the multinomial and bernoulli varieties
# with either pure counts or tfidf features
# train word2vec on all the texts - both training and test set
# we're not using test labels, just texts so this is fine

#Then try extra trees and svm
etree_w2v = Pipeline([
    ("word2vec vectorizer", MeanEmbeddingVectorizer(w2v)),
    ("extra trees", ExtraTreesClassifier(n_estimators=200))])
etree_w2v_sum = Pipeline([
        ("word2vec vectorizer", SumEmbeddingVectorizer(w2v)),
        ("extra trees", ExtraTreesClassifier(n_estimators=200))])
etree_w2v_tfidf = Pipeline([
    ("word2vec vectorizer", TfidfEmbeddingVectorizer(w2v)),
    ("extra trees", ExtraTreesClassifier(n_estimators=220))])
etree_w2v_tfidf_sum = Pipeline([
    ("word2vec vectorizer", TfidfEmbeddingVectorizerSum(w2v)),
    ("extra trees", ExtraTreesClassifier(n_estimators=200))])
svm_w2v = Pipeline([
    ("word2vec vectorizer", MeanEmbeddingVectorizer(w2v)),
    ("SVM", SVC())])
svm_w2v_tfdif = Pipeline([
    ("word2vec vectorizer", TfidfEmbeddingVectorizer(w2v)),
    ("SVM", SVC())])
svm_w2v_sum = Pipeline([
    ("word2vec vectorizer", SumEmbeddingVectorizer(w2v)),
    ("SVM", SVC())])
svm_w2v_tfdif_sum = Pipeline([
    ("word2vec vectorizer", TfidfEmbeddingVectorizerSum(w2v)),
    ("SVM", SVC())])

all_models = [
    ('etree_w2v', etree_w2v),
    ('etree_w2v_tfidf', etree_w2v_tfidf),
    ('svm_w2v', svm_w2v),
    ('svm_w2v_tfidf', svm_w2v_tfdif),
    ('etree_w2v_sum', etree_w2v_sum),
    ('etree_w2v_tfidf_sum', etree_w2v_tfidf_sum),
    ('svm_w2v_sum', svm_w2v_sum ),
    ('svm_w2v_tfdif_sum', svm_w2v_tfdif_sum)
]

print ('Benchmarking')
unsorted_scores = [(name, cross_val_score(model, X, y, cv=5).mean()) for name, model in all_models]
scores = sorted(unsorted_scores, key=lambda x: -x[1])
print (tabulate(scores, floatfmt=".4f", headers=("model", 'score')))

predictions = etree_w2v_tfidf.fit(Xtrain, ytrain).predict(Xtest)
for i in range(Xtest.shape[0]):
    print (Xtest[i])
    print (predictions[i])
    print (ytest[i])

print ('####################### TEST EXAMPLES #############################')
#Load in definetly unseen examples to test
#Load in the identifier setences
with open('testTrue.txt', 'r') as file:
    identifiers = file.read().splitlines()

with open('testExamples.txt', 'r') as file:
    nonId = file.read().splitlines()

#Load in the identifier sentences
X, y = [], []
j = 0
for id in identifiers:
    id = id.lower()                                         #Make everything lower case
    id = re.sub('['+string.punctuation+']', '', id)         #Strip out the punctuation
    words = id.split()
    X.append(words)
    y.append('1')
    j += 1
#Load in the none identifier sentences
i = 0
for id in nonId:
    id = id.lower()                                         #Make everything lower case
    id = re.sub('['+string.punctuation+']', '', id)         #Strip out the punctuation
    words = id.split()
    X.append(words)
    y.append('0')
    i += 1

X = np.array(X)
y = np.array(y)
#Shuffle X and y and split into testing and training sets
shuffler = np.random.permutation(range(X.shape[0]))
X = X[shuffler]
y = y[shuffler]

predictions = etree_w2v_tfidf.predict(X)
for i in range(X.shape[0]):
    print (X[i])
    print (predictions[i])
    print (y[i])

#Convert to int so can get the accuracy and recall etc
y = np.array(y).astype(int)
predictions = np.array(predictions).astype(int)

acc, recall, precision = make_stats(predictions, y)

print ('Accuracy = ', acc)
print ('Recall = ', recall)
print ('Precision =', precision)


from functools import wraps
from flask_login import current_user
from flask import abort, redirect, url_for, jsonify

def admin_required(func):
    @wraps(func)
    def decorator(*args, **kwargs):
        if not current_user.is_administrator:
            abort(404)
        return func(*args, **kwargs)

    return decorator

def base36(num, width=4):
    table = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'
    result = ''
    while num > 0:
        rem = num % 36
        num //= 36
        result = table[rem] + result
    length = len(result)
    if length < width:
        result = (width - length) * '0' + result
    return result

def chunk_string(string, width):
    import re
    return re.findall(r'.{%d}' % width, string)

def gen_ok_200(**kwargs):
    data = {'status': 200}
    for kwarg, value in kwargs.items():
        data[kwarg] = value
    return jsonify(data)

def gen_err_404():
    return jsonify({'status': 404,
                    'message': '所操作的Task不存在'})

def gen_err_500(e):
    return jsonify({'status': 500,
                    'message': e.__str__()
                    })


# -*- coding: utf-8 -*-
from __future__ import absolute_import, division, print_function, unicode_literals  # noqa
from typing import *  # noqa
from abc import ABCMeta, abstractmethod  # noqa

from typing import cast
import logging
from threading import Thread
import traceback

from pykka import ThreadingActor, ActorProxy
from gevent.pywsgi import WSGIServer

from crlib.env import env

from flask_app_public import app_public
from flask_app_nginx_rtmp import app_nginx_rtmp

LOGGER = logging.getLogger(__name__)

class WSGIServerRunnerInterface(object):
    __metaclass__ = ABCMeta

    @abstractmethod
    def is_running(self):
        # type: () -> bool
        pass

    @abstractmethod
    def run(self):
        # type: () -> None
        pass

class WSGIServerRunnerImplement(WSGIServerRunnerInterface):
    def __init__(self, server, server_name):
        # type: (WSGIServer, str) -> None
        super(WSGIServerRunnerImplement, self).__init__()
        self._hand = None  # type: Optional[Thread]
        self.server = server
        self.server_name = server_name

    @property
    def hand(self):
        # type: () -> Optional[Thread]
        return self._hand

    @hand.setter
    def hand(self, hand):
        # type: (Thread) -> None
        self._hand = hand

    def is_running(self):
        # type: () -> bool
        return self.hand is not None

    def server_forever(self):
        # type: () -> None
        try:
            self.server.serve_forever()
        except Exception:
            self.hand = None
            LOGGER.error(traceback.format_exc())

    def run(self):
        # type: () -> None
        if self.hand is None:
            LOGGER.info('run web server %s' % (self.server_name, ))
            self.hand = Thread(target=self.server_forever)
            self.hand.setDaemon(True)
            self.hand.start()
        else:
            LOGGER.error(
                'web server %s have been running' % (self.server_name, ))

class WSGIServerRunnerFactory(object):
    @classmethod
    def make(cls, server, server_name):
        # type: (WSGIServer, str) -> WSGIServerRunnerInterface
        o = WSGIServerRunnerImplement(
            server, server_name)  # type: WSGIServerRunnerInterface
        assert isinstance(o, WSGIServerRunnerInterface)
        return o

class WebServerAsyncInterface(object):
    __metaclass__ = ABCMeta

    @abstractmethod
    def run(self):
        # type: () -> None
        pass

    @property
    @abstractmethod
    def monitor_info(self):
        # type: () -> Dict[str, Any]
        pass

class WebServerAsyncImplement(
        ThreadingActor,  # type: ignore
        WebServerAsyncInterface):
    use_daemon_thread = True

    def __init__(self):
        # type: () -> None
        super(WebServerAsyncImplement, self).__init__()
        self.app_nginx_rtmp_server_runner = WSGIServerRunnerFactory.make(
            WSGIServer(('', self.APP_WEB_SERVER_NGINX_RTMP_PORT),
                       app_nginx_rtmp), 'nginx_rtmp')
        self.app_public_server_runner = WSGIServerRunnerFactory.make(
            WSGIServer(('', self.APP_WEB_SERVER_PUBLIC_PORT), app_public),
            'public')

    @property
    def monitor_info(self):
        # type: () -> Dict[str, Any]
        r = {
            'nginx_rtmp_server': {
                'is_running': self.app_nginx_rtmp_server_runner.is_running(),
            },
            'public_server': {
                'is_running': self.app_public_server_runner.is_running(),
            },
        }
        return r

    @property
    def APP_WEB_SERVER_NGINX_RTMP_IP(self):
        # type: () -> str
        return cast(str, env.get('APP_WEB_SERVER_NGINX_RTMP_IP'))

    @property
    def APP_WEB_SERVER_NGINX_RTMP_PORT(self):
        # type: () -> int
        return cast(int, env.get('APP_WEB_SERVER_NGINX_RTMP_PORT', type_=int))

    @property
    def APP_WEB_SERVER_PUBLIC_IP(self):
        # type: () -> str
        return cast(str, env.get('APP_WEB_SERVER_PUBLIC_IP'))

    @property
    def APP_WEB_SERVER_PUBLIC_PORT(self):
        # type: () -> int
        return cast(int, env.get('APP_WEB_SERVER_PUBLIC_PORT', type_=int))

    def run(self):
        # type: () -> None
        if not self.app_nginx_rtmp_server_runner.is_running():
            self.app_nginx_rtmp_server_runner.run()
        if not self.app_public_server_runner.is_running():
            self.app_public_server_runner.run()

class WebServerAsyncFactory(object):
    __proxy = None

    @classmethod
    def make(cls):
        # type: () -> WebServerAsyncInterface
        if cls.__proxy is None:
            cls.__proxy = WebServerAsyncImplement().start().proxy()
        assert isinstance(cls.__proxy, ActorProxy)
        return cls.__proxy  # type: ignore


from timeit import default_timer as time
from numba import cuda
import numpy as np

bpg = 256
tpb = 32

n = bpg * tpb

@cuda.jit
def matmul(A, B, C):
	tx = cuda.threadIdx.x
	ty = cuda.threadIdx.y
	bx = cuda.blockIdx.x
	by = cuda.blockIdx.y
	bw = cuda.blockDim.x
	bh = cuda.blockDim.y
	x = tx + bx * bw
	y = ty + by * bh
	if x >= n or y >= n:
		return
	C[y, x] = 0
	for i in range(n):
		C[y, x] += A[y, i] * B[i, x]

A = np.array(np.random.random((n, n)), dtype=np.float32)
B = np.array(np.random.random((n, n)), dtype=np.float32)
C = np.empty_like(A)
print("N = %d x %d" % (n, n))
stream = cuda.stream()
with stream.auto_synchronize():
	dA = cuda.to_device(A, stream)
	dB = cuda.to_device(B, stream)
	dC = cuda.to_device(C, stream)
	s = time()
	matmul[(bpg, bpg), (tpb, tpb), stream](dA, dB, dC)
	e = time()
	dC.to_host(stream)
tcuda = e - s
# Host compute
Amat = np.matrix(A)
Bmat = np.matrix(B)
s = time()
Cans = Amat * Bmat
e = time()
tcpu = e - s
# Check result
assert np.allclose(C, Cans)
print('cpu:  %f' % tcpu)
print('cuda: %f' % tcuda)
print('cuda speedup: %.2fx' % (tcpu / tcuda))


from django.db import models

from guardian.shortcuts import get_objects_for_user

from readthedocs.builds.constants import BRANCH
from readthedocs.builds.constants import TAG
from readthedocs.builds.constants import LATEST
from readthedocs.builds.constants import LATEST_VERBOSE_NAME
from readthedocs.builds.constants import STABLE
from readthedocs.builds.constants import STABLE_VERBOSE_NAME
from readthedocs.projects import constants

class ProjectManager(models.Manager):

    """
    Projects take into account their own privacy_level setting.
    """

    use_for_related_fields = True

    def _add_user_repos(self, queryset, user):
        if user.has_perm('projects.view_project'):
            return self.get_queryset().all().distinct()
        if user.is_authenticated():
            user_queryset = get_objects_for_user(user, 'projects.view_project')
            queryset = user_queryset | queryset
        return queryset.distinct()

    def for_user_and_viewer(self, user, viewer):
        """
        Show projects that a user owns, that another user can see.
        """
        queryset = self.filter(privacy_level=constants.PUBLIC)
        queryset = self._add_user_repos(queryset, viewer)
        queryset = queryset.filter(users__in=[user])
        return queryset

    def for_admin_user(self, user=None):
        if user.is_authenticated():
            return self.filter(users__in=[user])
        else:
            return self.none()

    def public(self, user=None):
        queryset = self.filter(privacy_level=constants.PUBLIC)
        if user:
            return self._add_user_repos(queryset, user)
        else:
            return queryset

    def protected(self, user=None):
        queryset = self.filter(privacy_level__in=[constants.PUBLIC, constants.PROTECTED])
        if user:
            return self._add_user_repos(queryset, user)
        else:
            return queryset

    def private(self, user=None):
        queryset = self.filter(privacy_level=constants.PRIVATE)
        if user:
            return self._add_user_repos(queryset, user)
        else:
            return queryset

    # Aliases

    def dashboard(self, user=None):
        return self.for_admin_user(user)

    def api(self, user=None):
        return self.public(user)

class VersionManager(models.Manager):

    """
    Versions take into account their own privacy_level setting.
    """

    use_for_related_fields = True

    def _add_user_repos(self, queryset, user):
        if user.has_perm('builds.view_version'):
            return self.get_queryset().all().distinct()
        if user.is_authenticated():
            user_queryset = get_objects_for_user(user, 'builds.view_version')
            queryset = user_queryset | queryset
        return queryset.distinct()

    def public(self, user=None, project=None, only_active=True):
        queryset = self.filter(privacy_level=constants.PUBLIC)
        if user:
            queryset = self._add_user_repos(queryset, user)
        if project:
            queryset = queryset.filter(project=project)
        if only_active:
            queryset = queryset.filter(active=True)
        return queryset

    def protected(self, user=None, project=None, only_active=True):
        queryset = self.filter(privacy_level__in=[constants.PUBLIC, constants.PROTECTED])
        if user:
            queryset = self._add_user_repos(queryset, user)
        if project:
            queryset = queryset.filter(project=project)
        if only_active:
            queryset = queryset.filter(active=True)
        return queryset

    def private(self, user=None, project=None, only_active=True):
        queryset = self.filter(privacy_level__in=[constants.PRIVATE])
        if user:
            queryset = self._add_user_repos(queryset, user)
        if project:
            queryset = queryset.filter(project=project)
        if only_active:
            queryset = queryset.filter(active=True)
        return queryset

    def api(self, user=None):
        return self.public(user, only_active=False)

    def create_stable(self, **kwargs):
        defaults = {
            'slug': STABLE,
            'verbose_name': STABLE_VERBOSE_NAME,
            'machine': True,
            'active': True,
            'identifier': STABLE,
            'type': TAG,
        }
        defaults.update(kwargs)
        return self.create(**defaults)

    def create_latest(self, **kwargs):
        defaults = {
            'slug': LATEST,
            'verbose_name': LATEST_VERBOSE_NAME,
            'machine': True,
            'active': True,
            'identifier': LATEST,
            'type': BRANCH,
        }
        defaults.update(kwargs)
        return self.create(**defaults)

class BuildManager(models.Manager):

    """
    Build objects take into account the privacy of the Version they relate to.
    """

    use_for_related_fields = True

    def _add_user_repos(self, queryset, user=None):
        if user.has_perm('builds.view_version'):
            return self.get_queryset().all().distinct()
        if user.is_authenticated():
            user_queryset = get_objects_for_user(user, 'builds.view_version')
            pks = [p.pk for p in user_queryset]
            queryset = self.get_queryset().filter(version__pk__in=pks) | queryset
        return queryset.distinct()

    def public(self, user=None, project=None):
        queryset = self.filter(version__privacy_level=constants.PUBLIC)
        if user:
            queryset = self._add_user_repos(queryset, user)
        if project:
            queryset = queryset.filter(project=project)
        return queryset

    def api(self, user=None):
        return self.public(user)

class RelatedProjectManager(models.Manager):

    """
    A manager for things that relate to Project and need to get their perms from the project.

    This shouldn't be used as a subclass.
    """
    use_for_related_fields = True
    project_field = 'project'

    def _add_user_repos(self, queryset, user=None):
        # Hack around get_objects_for_user not supporting global perms
        if user.has_perm('projects.view_project'):
            return self.get_queryset().all().distinct()
        if user.is_authenticated():
            # Add in possible user-specific views
            project_qs = get_objects_for_user(user, 'projects.view_project')
            pks = [p.pk for p in project_qs]
            kwargs = {'%s__pk__in' % self.project_field: pks}
            queryset = self.get_queryset().filter(**kwargs) | queryset
        return queryset.distinct()

    def public(self, user=None, project=None):
        kwargs = {'%s__privacy_level' % self.project_field: constants.PUBLIC}
        queryset = self.filter(**kwargs)
        if user:
            queryset = self._add_user_repos(queryset, user)
        if project:
            queryset = queryset.filter(project=project)
        return queryset

    def protected(self, user=None, project=None):
        kwargs = {
            '%s__privacy_level__in' % self.project_field: [constants.PUBLIC, constants.PROTECTED]
        }
        queryset = self.filter(**kwargs)
        if user:
            queryset = self._add_user_repos(queryset, user)
        if project:
            queryset = queryset.filter(project=project)
        return queryset

    def private(self, user=None, project=None):
        kwargs = {
            '%s__privacy_level' % self.project_field: constants.PRIVATE,
        }
        queryset = self.filter(**kwargs)
        if user:
            queryset = self._add_user_repos(queryset, user)
        if project:
            queryset = queryset.filter(project=project)
        return queryset

    def api(self, user=None):
        return self.public(user)

class ParentRelatedProjectManager(RelatedProjectManager):
    project_field = 'parent'
    use_for_related_fields = True

class ChildRelatedProjectManager(RelatedProjectManager):
    project_field = 'child'
    use_for_related_fields = True

class RelatedBuildManager(models.Manager):

    '''For models with association to a project through :py:class:`Build`'''

    use_for_related_fields = True

    def _add_user_repos(self, queryset, user=None):
        if user.has_perm('builds.view_version'):
            return self.get_queryset().all().distinct()
        if user.is_authenticated():
            user_queryset = get_objects_for_user(user, 'builds.view_version')
            pks = [p.pk for p in user_queryset]
            queryset = self.get_queryset().filter(
                build__version__pk__in=pks) | queryset
        return queryset.distinct()

    def public(self, user=None, project=None):
        queryset = self.filter(build__version__privacy_level=constants.PUBLIC)
        if user:
            queryset = self._add_user_repos(queryset, user)
        if project:
            queryset = queryset.filter(build__project=project)
        return queryset

    def api(self, user=None):
        return self.public(user)

class RelatedUserManager(models.Manager):

    """For models with relations through :py:class:`User`"""

    def api(self, user=None):
        """Return objects for user"""
        if not user.is_authenticated():
            return self.none()
        return self.filter(users=user)

class AdminPermission(object):

    @classmethod
    def is_admin(cls, user, project):
        return user in project.users.all()

    @classmethod
    def is_member(cls, user, obj):
        return user in obj.users.all()

class AdminNotAuthorized(ValueError):
    pass


from ..common import *
def default_mapping(r, c, up_or_down, w, h):
    return r*w + c

def full_kernel_connector(pre_layer_width, pre_layer_height, kernel,
                          exc_delay=2., inh_delay=1.,
                          col_step=1, row_step=1,
                          col_start=0, row_start=0,
                          map_to_src=default_mapping,
                          on_path=True,
                          min_w = 0.001, remove_inh_only=True):
    '''Create connection list based on a convolution kernel, the format
       for the lists is to be used with PyNN 0.7.
       (Pre neuron index, Post neuron index, weight, delay)

       :param layer_width: Pre layer width
       :param layer_height: Pre layer height
       :param kernel: Convolution kernel
       :param col_step: Skip this many columns (will reduce Post layer size)
       :param row_step: Skip this many rows (will reduce Post layer size)
       :param delay: How many time units will it take the spikes to
                     get from Pre to Post

       :return exc_conns: Excitatory connections list
       :return inh_conns: Inhibitory connections list
    '''
    layer_width, layer_height = pre_layer_width, pre_layer_height
    exc_conns = []
    inh_conns = []
    kh, kw = kernel.shape
    half_kh, half_kw = kh//2, kw//2
    num_dst = ( (layer_height - row_start)//row_step )*\
              ( (layer_width  - col_start)//col_step )

    exc_counts = [0 for dr in range(num_dst)]
    inh_counts = [0 for dr in range(num_dst)]

    for dr in range(row_start, layer_height, row_step):
        for dc in range(col_start, layer_width, col_step):

            sr0 = dr - half_kh
            sc0 = dc - half_kw

            for kr in range(kh):
                sr = sr0 + kr
                if sr < 0 or sr >= layer_height:
                    continue

                for kc in range(kw):
                    sc = sc0 + kc
                    if sc < 0 or sc >= layer_width:
                        continue

                    w = kernel[kr, kc]
                    if np.abs(w) < min_w:
                        continue

                    if on_path:
                        src = map_to_src(sr, sc, 1, layer_width, layer_height)
                    else:
                        src = map_to_src(sr, sc, 0, layer_width, layer_height)
                    # src = sr*layer_width + sc + src_start_idx
                    # divide values so that indices match the size of the
                    # Post (destination) next layer
                    dst = (dr//row_step)*(layer_width//col_step) + (dc//col_step)

                    if dst >= num_dst:
                        continue

                    src = np.uint32(src)
                    dst = np.uint32(dst)
                    w = float(w);

                    if w < 0:
                        inh_conns.append((src, dst, w, inh_delay))
                        inh_counts[dst] += 1
                    elif w > 0:
                        exc_conns.append((src, dst, w, exc_delay))
                        exc_counts[dst] += 1

    if remove_inh_only:
        exc_conns[:], inh_conns[:] = remove_inh_only_dst(exc_conns, inh_conns,
                                                         exc_counts)

    return exc_conns, inh_conns

def remove_inh_only_dst(exc_conns, inh_conns, exc_counts):
    new_exc = exc_conns[:] #copy lists --- paranoid choice
    new_inh = inh_conns[:] #copy lists --- paranoid choice

    for i in range(len(exc_counts)):
        if exc_counts[i] == 0:
            new_exc[:] = [x for x in new_exc if x[1] != i]
            new_inh[:] = [x for x in new_inh if x[1] != i]

    return new_exc, new_inh

def inh_neighbours(r, c, row_step, col_step, kw, kh, correlation,
                   delay=1, selfdelay=4):
    if row_step >= kh or col_step >= kw:
        return []
    else:
        hlf_kw = kw//2
        hlf_kh = kh//2
        src = (r//row_step)*(imgw//col_step) + c//col_step

        nbr = []
        kr = 0
        for nr in range(r - hlf_kh, r + hlf_kh + 1, row_step):
            if nr < 0 or nr >= imgh:
                kr += 1
                continue

            kc = 0
            for nc in range(c - hlf_kw, c + hlf_kw + 1, col_step):
                if nc < 0 or nc >= imgw:
                    kc += 1
                    continue

                if nr == r and nc == c:
                    d = selfdelay
                    dst = src
                else:
                    d = delay
                    dst = (nr//row_step)*(imgw//col_step) + nc//col_step

                w = correlation[kr, kc]

                nbr.append( (src, dst, d, w) )

                kc += 1
            kr += 1

        return nbr

def lateral_inh_connector(layer_width, layer_height, correlation,
                          inh_weight, delay=1., self_delay=4,
                          col_step=1, row_step=1,
                          col_start=0, row_start=0, min_w = 0.001):
    '''Assuming there's an interneuron layer with as many neurons as dest'''
    # exc_conns = []
    inh_conns = []
    kh, kw = correlation.shape
    half_kh, half_kw = kh//2, kw//2
    num_dst = ( (layer_height - row_start)//row_step )*\
              ( (layer_width  - col_start)//col_step )

    for dr in range(row_start, layer_height, row_step):
        for dc in range(col_start, layer_width, col_step):
            sr0 = dr - half_kh
            sc0 = dc - half_kw

            for kr in range(kh):
                sr = sr0 + kr
                if sr < 0 or sr >= layer_height:
                    continue

                for kc in range(kw):
                    sc = sc0 + kc
                    if sc < 0 or sc >= layer_width:
                        continue

                    #divide values so that indices match the size of the
                    #Post (destination) next layer
                    ### from Post to Interneurons
                    # src = (dr//row_step)*layer_width//col_step + (dc//col_step)
                    # exc_conns.append( (src, src, exc_weight, delay) )

                    ### from Interneurons to Post
                    inh_conns += inh_neighbours(dr, dc, row_step, col_step, kw, kh,
                                                correlation, delay, selfdelay)

    # return exc_conns, inh_conns
    return inh_conns


def add_tags(self, group, tags):
    from sentry.models import TagValue, GroupTagValue
    project_id = group.project_id
    date = group.last_seen
    for tag_item in tags:
        if (len(tag_item) == 2):
            ((key, value), data) = (tag_item, None)
        else:
            (key, value, data) = tag_item
        buffer.incr(TagValue, {
            'times_seen': 1,
        }, {
            'project_id': project_id,
            'key': key,
            'value': value,
        }, {
            'last_seen': date,
            'data': data,
        })
        buffer.incr(GroupTagValue, {
            'times_seen': 1,
        }, {
            'group_id': group.id,
            'key': key,
            'value': value,
        }, {
            'last_seen': date,
        })


import io
from datetime import datetime, timedelta
from typing import Optional

import dask.dataframe as dd
import pandas as pd

import dask_pipes as dp

TEST_DS: Optional[pd.DataFrame] = None

def get_ds() -> pd.DataFrame:
    global TEST_DS
    if TEST_DS is None:
        ds = """
        index c0 c1  c2   c3  c4   c5
        2     0  a   1    3.4 s1   2019-01-01T02:11:59
        2     1  a   100  5   s2
        1     0  b   9    nan s4   2019-02-01T20:33:32
        3     0  nan 100  -1  s4
        3     1  c   100  3.1 s4   2019-01-23T10:57:1
        """
        ds = dd.from_pandas(pd.read_csv(io.StringIO(ds), sep=r'\s+'), npartitions=3)
        # ds['c1'] = ds['c1'].apply(lambda x: x if x != 'b' else -1, meta=('c1',object)).compute()
        ds['c5'] = dd.to_datetime(ds['c5'])
        ds['c6'] = pd.Series([timedelta(1.12), None, timedelta(0), timedelta(-10)])
        ds['c7'] = datetime(2019, 7, 1)
        ds = ds.set_index('index').persist()
        # ds = ds.compute()
        TEST_DS = ds
    return TEST_DS.copy()

def test_pipeline():
    ds = get_ds()

    p = dp.pipes.prepareNN(date_retro_date_mapping={'c5': 'c7'})
    p.mixins = [dp.extensions.CacheMixin('tmp')]
    f_run = p.fit(ds)
    t_run = p.transform(ds)

    assert p.input_names == ['X', 'run_id']
    assert p.output_names == ['normalized']

def test_args():
    @dp.returns(['some_result'])
    def foo(a, a_default=None, *var_pos, b, b_default=None, **var_key):
        return (a, a_default, var_pos, b, b_default, var_key),

    p = dp.Pipeline()
    a = dp.as_node(foo, name='a')

    p['a'] >> a['a']
    p['a_default'] >> a['a_default']
    p['args'] >> a['var_pos']
    p['b'] >> a['b']
    p['kwargs'] >> a['var_key']
    a['some_result'] >> p['result']

    run = p.transform(1, 2, 3, 4, b=10, kwarg=10)
    assert run.outputs['result'] == (1, 2, (3, 4), 10, None, {'kwarg': 10})

if __name__ == '__main__':
    test_pipeline()
    test_args()


#!/usr/bin/env python3

import pytator
import json
import os
import sys
import time
import subprocess
import sys

if __name__ == '__main__':
    rest_svc = os.getenv('TATOR_API_SERVICE')
    work_dir = os.getenv('TATOR_WORK_DIR')
    token = os.getenv('TATOR_AUTH_TOKEN')
    project_id = os.getenv('TATOR_PROJECT_ID')
    pipeline_args_str = os.getenv('TATOR_PIPELINE_ARGS')
    if pipeline_args_str:
        pipeline_args = json.loads(pipeline_args_str)
    else:
        print("ERROR: No pipeline arguments specified!")
        sys.exit(-1)
    box_type_id = pipeline_args['type_id']
    img_ext = pipeline_args.get('img_ext', None)
    media_type = pipeline_args.get('media_type', None)
    image_ext = pipeline_args.get('img_ext', None)
    species_attr_name = pipeline_args.get('species_attr_name','Species')
    confidence_attr_name = pipeline_args.get('confidence_attr_name','Confidence')
    optional_args=[]
    version_number = pipeline_args.get('version_number', None)
    if version_number:
        optional_args.extend(['--version-number', str(version_number)])

    args = ["python3",
            "/scripts/uploadToTator.py",
            "--url", rest_svc,
            "--project", str(project_id),
            "--token", token,
            "--img-base-dir", "/work",
            "--localization-type-id", str(box_type_id),
            "--media-type-id", str(0),
            '--media-type', media_type,
            *optional_args,
            '--img-ext', image_ext,
            '--species-attr-name', species_attr_name,
            '--confidence-attr-name', confidence_attr_name,
            '--train-ini', '/work/network/train.ini',
            '/work/results.csv']
    cmd = " ".join(args)
    print(f"Upload Command = '{cmd}'")
    p=subprocess.Popen(args)
    p.wait()
    sys.exit(p.returncode)


import sublime
import sublime_plugin
import subprocess
import platform

class OpenWithVimCommand(sublime_plugin.WindowCommand):
    def get_path(self):
        if self.window.active_view():
            return self.window.active_view().file_name()
        else:
            sublime.error_message(__name__ + ": No file to open.")
            return None

    def run(self):
        path = self.get_path()
        if path is not None:
            self.open_vim(path)

    def open_vim(self, path):
        local_os = platform.system()
        if local_os == 'Linux':
            terminal = "gnome-terminal"
            option = "--command"
            vim = "/usr/bin/vim"
            args = [terminal, option, vim + ' "' + path + '"']
        else:
            sublime.error_message("Sorry, I don't know how to open terminal vim on this platform: %s. Try open_with_gvim" % local_os)
            return
        subprocess.Popen(args)

class OpenWithGvimCommand(OpenWithVimCommand):
    def open_vim(self, path):
        local_os = platform.system()
        if local_os == 'Darwin':
            args = ["mvim", path]
        else:
            args = ["gvim", path]
        subprocess.Popen(args)


from django.shortcuts import render
from listings.models import Listing
from realtors.models import Realtor
from listings.choice import price_choices,bedroom_choices,state_choices
def index(request):
    listings=Listing.objects.order_by('-list_date').filter(is_published=True)[:3]
   # paginator = Paginator(listings,6)
    #page = request.GET.get('page')
    #paged_listings=paginator.get_page(page)

    context={
        'listings':listings,
        'state_choices':state_choices,
        'price_choices':price_choices,
        'bedroom_choices':bedroom_choices

    }
    return render(request,'pages/index.html',context)

def about(request):
    #get all realtors
    realtors=Realtor.objects.order_by('-hire_date')
    #get MVP
    mvp_realtors=Realtor.objects.all().filter(is_mvp=True)

    context={
        'realtors':realtors,
        'mvp_realtors': mvp_realtors
    }
    return render(request,'pages/about.html', context)
# Create your views here.


import sys
import os
curDir = os.getcwd()
desDir1 = os.path.abspath(os.path.join(os.path.dirname(__file__), '../..'))
desDir2 = os.path.abspath(os.path.join(os.path.dirname(__file__), '../venv'))
desDir3 = os.path.abspath(os.path.join(os.path.dirname(__file__), '../venv/Scripts/python36.zip'))
desDir4 = os.path.abspath(os.path.join(os.path.dirname(__file__), '../venv/lib'))
desDir5 = os.path.abspath(os.path.join(os.path.dirname(__file__), '../venv/lib/site-packages'))
desDir6 = os.path.abspath(os.path.join(os.path.dirname(__file__), '../venv/lib/site-packages/setuptools-40.8.0-py3.6.egg'))
desDir7 = os.path.abspath(os.path.join(os.path.dirname(__file__), '../venv/lib/site-packages/pip-19.0.3-py3.6.egg'))
desDir8 = os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))
desDir9 = os.path.abspath(os.path.join(os.path.dirname(__file__), '../lib'))

sys.path.extend([desDir1,desDir2,desDir3,desDir4,desDir5,desDir6,desDir7,desDir8,desDir9])

#Below is code used to troubleshoot env problems
# path = sys.path
# print('curDir:', curDir)
# print('desDir1:', desDir1)
# print('desDir2:', desDir2)
# print('desDir3:', desDir3)
# print('desDir4:', desDir4)
# print('desDir5:', desDir5)
# print('desDir6:', desDir6)
# print('desDir7:', desDir7)
# print('desDir8:', desDir8)
# print('desDir9:', desDir9)
# print('\n')
# print(len(path))
# print('\n')
# for i in path:
#     print(path.index(i), i)

from PyQt5 import QtCore, QtGui, QtWidgets
from lib import IMayFunctions

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(768, 589)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Preferred)
        sizePolicy.setHorizontalStretch(0)
        sizePolicy.setVerticalStretch(0)
        sizePolicy.setHeightForWidth(self.centralwidget.sizePolicy().hasHeightForWidth())
        self.centralwidget.setSizePolicy(sizePolicy)
        self.centralwidget.setObjectName("centralwidget")
        self.gridLayout_2 = QtWidgets.QGridLayout(self.centralwidget)
        self.gridLayout_2.setSizeConstraint(QtWidgets.QLayout.SetMaximumSize)
        self.gridLayout_2.setObjectName("gridLayout_2")
        self.listWidget_1 = QtWidgets.QListWidget(self.centralwidget)
        self.listWidget_1.setObjectName("listWidget_1")
        self.gridLayout_2.addWidget(self.listWidget_1, 1, 0, 1, 3)
        self.listWidget_2 = QtWidgets.QListWidget(self.centralwidget)
        self.listWidget_2.setObjectName("listWidget_2")
        self.gridLayout_2.addWidget(self.listWidget_2, 2, 0, 1, 3)
        self.comboBox = QtWidgets.QComboBox(self.centralwidget)
        sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Preferred)
        sizePolicy.setHorizontalStretch(0)
        sizePolicy.setVerticalStretch(0)
        sizePolicy.setHeightForWidth(self.comboBox.sizePolicy().hasHeightForWidth())
        self.comboBox.setSizePolicy(sizePolicy)
        self.comboBox.setObjectName("comboBox")
        self.gridLayout_2.addWidget(self.comboBox, 0, 3, 1, 1)
        self.lineEdit = QtWidgets.QLineEdit(self.centralwidget)
        self.lineEdit.setObjectName("lineEdit")
        self.gridLayout_2.addWidget(self.lineEdit, 0, 2, 1, 1)
        self.addDirectory = QtWidgets.QToolButton(self.centralwidget)
        self.addDirectory.setObjectName("addDirectory")
        self.gridLayout_2.addWidget(self.addDirectory, 0, 0, 1, 1)
        self.formLayout = QtWidgets.QFormLayout()
        self.formLayout.setFormAlignment(QtCore.Qt.AlignBottom|QtCore.Qt.AlignLeading|QtCore.Qt.AlignLeft)
        self.formLayout.setSpacing(0)
        self.formLayout.setObjectName("formLayout")
        self.Debug_1 = QtWidgets.QPushButton(self.centralwidget)
        self.Debug_1.setObjectName("Debug_1")
        self.formLayout.setWidget(0, QtWidgets.QFormLayout.LabelRole, self.Debug_1)
        self.Debug_2 = QtWidgets.QPushButton(self.centralwidget)
        self.Debug_2.setObjectName("Debug_2")
        self.formLayout.setWidget(0, QtWidgets.QFormLayout.FieldRole, self.Debug_2)
        self.Debug_3 = QtWidgets.QPushButton(self.centralwidget)
        self.Debug_3.setObjectName("Debug_3")
        self.formLayout.setWidget(1, QtWidgets.QFormLayout.LabelRole, self.Debug_3)
        self.Debug_4 = QtWidgets.QPushButton(self.centralwidget)
        self.Debug_4.setObjectName("Debug_4")
        self.formLayout.setWidget(1, QtWidgets.QFormLayout.FieldRole, self.Debug_4)
        self.Debug_5 = QtWidgets.QPushButton(self.centralwidget)
        self.Debug_5.setObjectName("Debug_5")
        self.formLayout.setWidget(2, QtWidgets.QFormLayout.LabelRole, self.Debug_5)
        self.Debug_6 = QtWidgets.QPushButton(self.centralwidget)
        self.Debug_6.setObjectName("Debug_6")
        self.formLayout.setWidget(2, QtWidgets.QFormLayout.FieldRole, self.Debug_6)
        self.NextButton = QtWidgets.QPushButton(self.centralwidget)
        self.NextButton.setObjectName("NextButton")
        self.formLayout.setWidget(3, QtWidgets.QFormLayout.LabelRole, self.NextButton)
        self.CancelButton = QtWidgets.QPushButton(self.centralwidget)
        self.CancelButton.setObjectName("CancelButton")
        self.formLayout.setWidget(3, QtWidgets.QFormLayout.FieldRole, self.CancelButton)
        self.gridLayout_2.addLayout(self.formLayout, 2, 3, 1, 1)
        self.toolButton = QtWidgets.QToolButton(self.centralwidget)
        icon = QtGui.QIcon()
        icon.addPixmap(QtGui.QPixmap("folder1.jpg"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
        self.toolButton.setIcon(icon)
        self.toolButton.setObjectName("toolButton")
        self.gridLayout_2.addWidget(self.toolButton, 0, 1, 1, 1)
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 768, 26))
        self.menubar.setObjectName("menubar")
        self.menuIMay_prototype = QtWidgets.QMenu(self.menubar)
        self.menuIMay_prototype.setObjectName("menuIMay_prototype")
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)
        self.menubar.addAction(self.menuIMay_prototype.menuAction())

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

        self.Debug_1.clicked.connect(lambda: IMayFunctions.rename())
        self.Debug_2.clicked.connect(lambda: IMayFunctions.zip_files())
        self.toolButton.clicked.connect(lambda: self.directory_dialog())
        self.addDirectory.clicked.connect(lambda: self.add_directory())
        self.Debug_3.pressed.connect(lambda: self.directory_dialog())

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
        self.addDirectory.setText(_translate("MainWindow", "+"))
        self.Debug_1.setText(_translate("MainWindow", "Debug_1"))
        self.Debug_2.setText(_translate("MainWindow", "Debug_2"))
        self.Debug_3.setText(_translate("MainWindow", "Debug_3"))
        self.Debug_4.setText(_translate("MainWindow", "Debug_4"))
        self.Debug_5.setText(_translate("MainWindow", "Debug_5"))
        self.Debug_6.setText(_translate("MainWindow", "Debug_6"))
        self.NextButton.setText(_translate("MainWindow", "Next"))
        self.CancelButton.setText(_translate("MainWindow", "Cancel"))
        self.toolButton.setText(_translate("MainWindow", "..."))
        self.menuIMay_prototype.setTitle(_translate("MainWindow", "IMay prototype"))

    def directory_dialog(self):
        path_to_directory = QtWidgets.QFileDialog.getExistingDirectory(None, "Choose Directory", "F:\Senior_2")
        self.lineEdit.setText(path_to_directory)

    def add_directory(self):
        path_to_directory = self.lineEdit.text()
        self.listWidget_1.addItem(path_to_directory)

if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    MainWindow = QtWidgets.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())


# 然后在另一台机器上启动任务进程（本机上启动也可以）
import time,sys,queue
from multiprocessing.managers import BaseManager

# 创建类似的QueueManager
class QueueManager(BaseManager):
    pass

# 由于这个QueueManager只从网络上获取Queue，所以注册时只提供名字：
QueueManager.register('get_task_queue')
QueueManager.register('get_result_queue')

# 连接到服务器，也就是运行task_manager.py机器
server_addr='127.0.0.1'
print('Connect to server %s...' %server_addr)
# 端口和验证码注意保持与task_master.py设置的完全一致：
m=QueueManager(address=(server_addr,5000),authkey=b'abc')
# 从网络连接
m.connect()
# 获取Queue的对象
task=m.get_task_queue()
result=m.get_result_queue()

# 从task队列取任务，并把结果写入result队列
for i in range(10):
    try:
        n=task.get(timeout=1) # 没有数据将会等待timeout等待时间
        print('run task %d * %d...' %(n,n))
        r='%d * %d = %d' %(n,n,n*n)
        time.sleep(1)
        result.put(r)
    except queue.Empty:
        print('task queue is empty.')

# 处理结束
print('worker exit.')

"""
Queue之所以能通过网络访问，就是通过QueueManager实现的。由于QueueManager管理的不止一个Queue，所以，
要给每个Queue的网络调用接口起个名字，比如get_task_queue。

authkey是为了保证两台机器正常通信，不被其他机器恶意干扰。如果task_worker.py的authkey和
task_master.py的authkey不一致，肯定连接不上

Queue的作用是用来传递任务和接收结果，每个任务的描述数据量要尽量小。比如发送一个处理日志文件的任务，
就不要发送几百兆的日志文件本身，而是发送日志文件存放的完整路径，由Worker进程再去共享的磁盘上读取文件
"""


"""Utility Functions"""
import logging
import sys

def get_logger(name, log_level=logging.DEBUG):
    logger = logging.getLogger(name)
    if not logger.handlers:
        logger.setLevel(log_level)
        ch = logging.StreamHandler(sys.stdout)
        ch.setLevel(log_level)
        formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
        ch.setFormatter(formatter)
        logger.addHandler(ch)
    return logger

def log_method(method):
    def wrapped(self, *args, **kwargs):
        self.logger.info('Entering %s' % method.__name__)
        return method(self, *args, **kwargs)
    return wrapped


import unittest
from pprint import pprint

from checks import dns_resolution
from checks.config import Config

class TestDNSResolution(unittest.TestCase):

    def test_google(self):
        """Resolves www.google.com"""
        url = 'https://www.google.com/'
        config = Config(urls=[url])
        checker = dns_resolution.Checker(config=config, previous_results={})
        result = checker.run()

        self.assertIn(url, result)
        self.assertEqual(result[url]['hostname'], 'www.google.com')
        self.assertTrue(result[url], 'resolvable')
        self.assertIsInstance(result[url]['ipv4_addresses'], list)
        self.assertNotEqual(result[url]['ipv4_addresses'], [])

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


__author__ = 'yuandian'
#coding:utf8

from Tkinter import *
#在linux上定义环境变量

top = Tk() #创建一个窗口,赋值给top这变量
# 界面,我们看到一个进程 线程
top.title("源点信息科技")
top.geometry('600x400')  #设置标签字体的初始大
top.resizable(width=False,height=False)
label=Label(top,text='Hello world!',font='Helvetica -12 bold')
label.pack()
top.mainloop() #不停的循环给系统发送消息   (我要给桌面不停的创建窗口)


from django.test import TestCase, Client
from django.urls import reverse, resolve
from users.views import *
from django.contrib.auth import views as auth_views
from django.contrib import admin

class UrlTest(TestCase):
	def setUp(self):
		self.client = Client()

	def test_register(self):
		# path('', user_views.register, name='register'),
		url = reverse('register')
		self.assertEqual(resolve(url).func, register)
		self.assertEqual(resolve(url).url_name, "register")
		response = self.client.get(reverse('register'))

	def test_profile(self):
		# path('profile/', views.profile, name='profile'),
		url = reverse('profile')
		self.assertEqual(resolve(url).func, profile)
		self.assertEqual(resolve(url).url_name, "profile")

		# response = self.client.get(reverse('profile'))
		# self.assertEqual(response.status_code, 200)
		# self.assertTemplateUsed(response, 'userse/profile.html')

	def test_logout(self):
		# path('logout/', user_views.views.logout, name='profile'),
		url = reverse('logout')
		self.assertEqual(resolve(url).func, logout_view)
		self.assertEqual(resolve(url).url_name, "logout")

		# response = self.client.get(reverse('logout'))
		# self.assertEqual(response.status_code, 200)
		# self.assertTemplateUsed(response, 'userse/logout.html')

	# def test_login(self):
	# 	# path('logout/', user_views.views.logout, name='profile'),
	# 	url = reverse('login')
	# 	self.assertEqual(resolve(url).func,  auth_views.LoginView.as_view(template_name='users/login.html', redirect_authenticated_user=True))
	# 	self.assertEqual(resolve(url).url_name, "login")

	# 	# response = self.client.get(reverse('logout'))
	# 	# self.assertEqual(response.status_code, 200)
	# 	# self.assertTemplateUsed(response, 'userse/logout.html')

	# def test_password_reset(self):
	# 	# path('logout/', user_views.views.logout, name='profile'),
	# 	url = reverse('password_reset')
	# 	self.assertEqual(resolve(url).func, auth_PasswordResetView.as_view(template_name='users/password_reset.html'))
	# 	self.assertEqual(resolve(url).url_name, "password_reset")

	# 	# response = self.client.get(reverse('logout'))
	# 	# self.assertEqual(response.status_code, 200)
	# 	# self.assertTemplateUsed(response, 'userse/logout.html')


class Node():
  def __init__(self, data):
    self.data = data
    self.next = None

class LinkedList():
  def __init__(self):
    self.head = None

  # Print
  def print(self):
    cur_node = self.head
    while cur_node:
      print(cur_node.data)
      cur_node = cur_node.next

  # Append
  def append(self, data):
    new_node = Node(data)
    if self.head is None:
      self.head = new_node
      return
    last_node = self.head
    while last_node.next:
      last_node = last_node.next
    last_node.next = new_node

  # Prepend
  def prepend(self, data):
    new_node = Node(data)
    new_node.next = self.head
    self.head = new_node

  # Insert After Node
  def insert_after_node(self, prev, data):
    if not prev:
      print("previous node doesn't exist")
    new_node = Node(data)
    new_node.next = prev.next
    prev.next = new_node

  # Delete Value
  def delete_value(self, key):
    cur_node = self.head
    if cur_node and cur_node.data == key:
      self.head = cur_node.next
      cur_node = None
      return
    prev = None
    while cur_node and cur_node.data != key:
      prev = cur_node
      cur_node = cur_node.next
    if cur_node is None:
      return
    prev.next = cur_node.next
    cur_node = None

  # Delete at Position
  def delete_pos(self, pos):
    if self.head:
      cur_node = self.head
      if pos == 0:
        self.head = cur_node.next
        cur_node = None
        return

    prev = None
    count = 0
    while cur_node and count != pos:
      prev = cur_node
      cur_node = cur_node.next
      count += 1
    if cur_node is None:
      return
    prev.next = cur_node.next
    cur_node = None

  # Length Iterative
  def length_iterative(self):
    len = 0
    cur_node = self.head
    while cur_node:
      len+= 1
      cur_node = cur_node.next
    return len

  # Length Recursive
  def length_recursive(self, node):
    if node is None:
      return 0
    return 1 + self.length_recursive(node.next)

myLL = LinkedList()
myLL.append("A")
myLL.append("B")
myLL.append("C")
myLL.prepend("D")
myLL.insert_after_node(myLL.head, "E")
myLL.delete_value("B")
myLL.delete_pos(1)

# DAC
myLL.print()
print(myLL.length_iterative())
print(myLL.length_recursive(myLL.head))