"""
Crawl the storage server shares.

Ported to Python 3.
"""

from __future__ import unicode_literals
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

from future.utils import PY2, PY3
if PY2:
    from builtins import filter, map, zip, ascii, chr, hex, input, next, oct, open, pow, round, super, bytes, dict, list, object, range, str, max, min  # noqa: F401

import os
import time
import json
import struct
from twisted.internet import reactor
from twisted.application import service
from twisted.python.filepath import FilePath
from allmydata.storage.common import si_b2a
from allmydata.util import fileutil

class TimeSliceExceeded(Exception):
    pass

class MigratePickleFileError(Exception):
    """
    A pickle-format file exists (the FilePath to the file will be the
    single arg).
    """
    pass

def _convert_cycle_data(state):
    """
    :param dict state: cycle-to-date or history-item state

    :return dict: the state in the JSON form
    """

    def _convert_expiration_mode(value):
        # original is a 4-tuple, with the last element being a 2-tuple
        # .. convert both to lists
        return [
            value[0],
            value[1],
            value[2],
            list(value[3]),
        ]

    def _convert_lease_age(value):
        # if we're in cycle-to-date, this is a dict
        if isinstance(value, dict):
            return {
                "{},{}".format(k[0], k[1]): v
                for k, v in value.items()
            }
        # otherwise, it's a history-item and they're 3-tuples
        return [
            list(v)
            for v in value
        ]

    converters = {
        "configured-expiration-mode": _convert_expiration_mode,
        "cycle-start-finish-times": list,
        "lease-age-histogram": _convert_lease_age,
        "corrupt-shares": lambda value: [
            list(x)
            for x in value
        ],
        "leases-per-share-histogram": lambda value: {
            str(k): v
            for k, v in value.items()
        },
    }
    return {
            k: converters.get(k, lambda z: z)(v)
            for k, v in state.items()
    }

def _convert_pickle_state_to_json(state):
    """
    :param dict state: the pickled state

    :return dict: the state in the JSON form
    """
    assert state["version"] == 1, "Only known version is 1"

    converters = {
        "cycle-to-date": _convert_cycle_data,
    }
    return {
        k: converters.get(k, lambda x: x)(v)
        for k, v in state.items()
    }

def _upgrade_pickle_to_json(state_path, convert_pickle):
    """
    :param FilePath state_path: the filepath to ensure is json

    :param Callable[dict] convert_pickle: function to change
        pickle-style state into JSON-style state

    :returns FilePath: the local path where the state is stored

    If this state is pickle, convert to the JSON format and return the
    JSON path.
    """
    json_state_path = state_path.siblingExtension(".json")

    # if there's no file there at all, we're done because there's
    # nothing to upgrade
    if not state_path.exists():
        return json_state_path

    # upgrade the pickle data to JSON
    import pickle
    with state_path.open("rb") as f:
        state = pickle.load(f)
    new_state = convert_pickle(state)
    _dump_json_to_file(new_state, json_state_path)

    # we've written the JSON, delete the pickle
    state_path.remove()
    return json_state_path

def _confirm_json_format(fp):
    """
    :param FilePath fp: the original (pickle) name of a state file

    This confirms that we do _not_ have the pickle-version of a
    state-file and _do_ either have nothing, or the JSON version. If
    the pickle-version exists, an exception is raised.

    :returns FilePath: the JSON name of a state file
    """
    if fp.path.endswith(".json"):
        return fp
    jsonfp = fp.siblingExtension(".json")
    if fp.exists():
        raise MigratePickleFileError(fp)
    return jsonfp

def _dump_json_to_file(js, afile):
    """
    Dump the JSON object `js` to the FilePath `afile`
    """
    with afile.open("wb") as f:
        data = json.dumps(js)
        if PY2:
            f.write(data)
        else:
            f.write(data.encode("utf8"))

class _LeaseStateSerializer(object):
    """
    Read and write state for LeaseCheckingCrawler. This understands
    how to read the legacy pickle format files and upgrade them to the
    new JSON format (which will occur automatically).
    """

    def __init__(self, state_path):
        self._path = _confirm_json_format(FilePath(state_path))

    def load(self):
        """
        :returns: deserialized JSON state
        """
        with self._path.open("rb") as f:
            return json.load(f)

    def save(self, data):
        """
        Serialize the given data as JSON into the state-path
        :returns: None
        """
        tmpfile = self._path.siblingExtension(".tmp")
        _dump_json_to_file(data, tmpfile)
        fileutil.move_into_place(tmpfile.path, self._path.path)
        return None

class ShareCrawler(service.MultiService):
    """A ShareCrawler subclass is attached to a StorageServer, and
    periodically walks all of its shares, processing each one in some
    fashion. This crawl is rate-limited, to reduce the IO burden on the host,
    since large servers can easily have a terabyte of shares, in several
    million files, which can take hours or days to read.

    Once the crawler starts a cycle, it will proceed at a rate limited by the
    allowed_cpu_percentage= and cpu_slice= parameters: yielding the reactor
    after it has worked for 'cpu_slice' seconds, and not resuming right away,
    always trying to use less than 'allowed_cpu_percentage'.

    Once the crawler finishes a cycle, it will put off starting the next one
    long enough to ensure that 'minimum_cycle_time' elapses between the start
    of two consecutive cycles.

    We assume that the normal upload/download/get_buckets traffic of a tahoe
    grid will cause the prefixdir contents to be mostly cached in the kernel,
    or that the number of buckets in each prefixdir will be small enough to
    load quickly. A 1TB allmydata.com server was measured to have 2.56M
    buckets, spread into the 1024 prefixdirs, with about 2500 buckets per
    prefix. On this server, each prefixdir took 130ms-200ms to list the first
    time, and 17ms to list the second time.

    To use a crawler, create a subclass which implements the process_bucket()
    method. It will be called with a prefixdir and a base32 storage index
    string. process_bucket() must run synchronously. Any keys added to
    self.state will be preserved. Override add_initial_state() to set up
    initial state keys. Override finished_cycle() to perform additional
    processing when the cycle is complete. Any status that the crawler
    produces should be put in the self.state dictionary. Status renderers
    (like a web page which describes the accomplishments of your crawler)
    will use crawler.get_state() to retrieve this dictionary; they can
    present the contents as they see fit.

    Then create an instance, with a reference to a StorageServer and a
    filename where it can store persistent state. The statefile is used to
    keep track of how far around the ring the process has travelled, as well
    as timing history to allow the pace to be predicted and controlled. The
    statefile will be updated and written to disk after each time slice (just
    before the crawler yields to the reactor), and also after each cycle is
    finished, and also when stopService() is called. Note that this means
    that a crawler which is interrupted with SIGKILL while it is in the
    middle of a time slice will lose progress: the next time the node is
    started, the crawler will repeat some unknown amount of work.

    The crawler instance must be started with startService() before it will
    do any work. To make it stop doing work, call stopService().
    """

    slow_start = 300 # don't start crawling for 5 minutes after startup
    # all three of these can be changed at any time
    allowed_cpu_percentage = .10 # use up to 10% of the CPU, on average
    cpu_slice = 1.0 # use up to 1.0 seconds before yielding
    minimum_cycle_time = 300 # don't run a cycle faster than this

    def __init__(self, server, statefile, allowed_cpu_percentage=None):
        service.MultiService.__init__(self)
        if allowed_cpu_percentage is not None:
            self.allowed_cpu_percentage = allowed_cpu_percentage
        self.server = server
        self.sharedir = server.sharedir
        self._state_serializer = _LeaseStateSerializer(statefile)
        self.prefixes = [si_b2a(struct.pack(">H", i << (16-10)))[:2]
                         for i in range(2**10)]
        if PY3:
            # On Python 3 we expect the paths to be unicode, not bytes.
            self.prefixes = [p.decode("ascii") for p in self.prefixes]
        self.prefixes.sort()
        self.timer = None
        self.bucket_cache = (None, [])
        self.current_sleep_time = None
        self.next_wake_time = None
        self.last_prefix_finished_time = None
        self.last_prefix_elapsed_time = None
        self.last_cycle_started_time = None
        self.last_cycle_elapsed_time = None
        self.load_state()

    def minus_or_none(self, a, b):
        if a is None:
            return None
        return a-b

    def get_progress(self):
        """I return information about how much progress the crawler is
        making. My return value is a dictionary. The primary key is
        'cycle-in-progress': True if the crawler is currently traversing the
        shares, False if it is idle between cycles.

        Note that any of these 'time' keys could be None if I am called at
        certain moments, so application code must be prepared to tolerate
        this case. The estimates will also be None if insufficient data has
        been gatherered to form an estimate.

        If cycle-in-progress is True, the following keys will be present::

         cycle-complete-percentage': float, from 0.0 to 100.0, indicating how
                                     far the crawler has progressed through
                                     the current cycle
         remaining-sleep-time: float, seconds from now when we do more work
         estimated-cycle-complete-time-left:
                float, seconds remaining until the current cycle is finished.
                TODO: this does not yet include the remaining time left in
                the current prefixdir, and it will be very inaccurate on fast
                crawlers (which can process a whole prefix in a single tick)
         estimated-time-per-cycle: float, seconds required to do a complete
                                   cycle

        If cycle-in-progress is False, the following keys are available::

         next-crawl-time: float, seconds-since-epoch when next crawl starts
         remaining-wait-time: float, seconds from now when next crawl starts
         estimated-time-per-cycle: float, seconds required to do a complete
                                   cycle
        """

        d = {}

        if self.state["current-cycle"] is None:
            d["cycle-in-progress"] = False
            d["next-crawl-time"] = self.next_wake_time
            d["remaining-wait-time"] = self.minus_or_none(self.next_wake_time,
                                                          time.time())
        else:
            d["cycle-in-progress"] = True
            pct = 100.0 * self.last_complete_prefix_index / len(self.prefixes)
            d["cycle-complete-percentage"] = pct
            remaining = None
            if self.last_prefix_elapsed_time is not None:
                left = len(self.prefixes) - self.last_complete_prefix_index
                remaining = left * self.last_prefix_elapsed_time
                # TODO: remainder of this prefix: we need to estimate the
                # per-bucket time, probably by measuring the time spent on
                # this prefix so far, divided by the number of buckets we've
                # processed.
            d["estimated-cycle-complete-time-left"] = remaining
            # it's possible to call get_progress() from inside a crawler's
            # finished_prefix() function
            d["remaining-sleep-time"] = self.minus_or_none(self.next_wake_time,
                                                           time.time())
        per_cycle = None
        if self.last_cycle_elapsed_time is not None:
            per_cycle = self.last_cycle_elapsed_time
        elif self.last_prefix_elapsed_time is not None:
            per_cycle = len(self.prefixes) * self.last_prefix_elapsed_time
        d["estimated-time-per-cycle"] = per_cycle
        return d

    def get_state(self):
        """I return the current state of the crawler. This is a copy of my
        state dictionary.

        If we are not currently sleeping (i.e. get_state() was called from
        inside the process_prefixdir, process_bucket, or finished_cycle()
        methods, or if startService has not yet been called on this crawler),
        these two keys will be None.

        Subclasses can override this to add computed keys to the return value,
        but don't forget to start with the upcall.
        """
        state = self.state.copy() # it isn't a deepcopy, so don't go crazy
        return state

    def load_state(self):
        # we use this to store state for both the crawler's internals and
        # anything the subclass-specific code needs. The state is stored
        # after each bucket is processed, after each prefixdir is processed,
        # and after a cycle is complete. The internal keys we use are:
        #  ["version"]: int, always 1
        #  ["last-cycle-finished"]: int, or None if we have not yet finished
        #                           any cycle
        #  ["current-cycle"]: int, or None if we are sleeping between cycles
        #  ["current-cycle-start-time"]: int, seconds-since-epoch of when this
        #                                cycle was started, possibly by an earlier
        #                                process
        #  ["last-complete-prefix"]: str, two-letter name of the last prefixdir
        #                            that was fully processed, or None if we
        #                            are sleeping between cycles, or if we
        #                            have not yet finished any prefixdir since
        #                            a cycle was started
        #  ["last-complete-bucket"]: str, base32 storage index bucket name
        #                            of the last bucket to be processed, or
        #                            None if we are sleeping between cycles
        try:
            state = self._state_serializer.load()
        except Exception:
            state = {"version": 1,
                     "last-cycle-finished": None,
                     "current-cycle": None,
                     "last-complete-prefix": None,
                     "last-complete-bucket": None,
                     }
        state.setdefault("current-cycle-start-time", time.time()) # approximate
        self.state = state
        lcp = state["last-complete-prefix"]
        if lcp == None:
            self.last_complete_prefix_index = -1
        else:
            self.last_complete_prefix_index = self.prefixes.index(lcp)
        self.add_initial_state()

    def add_initial_state(self):
        """Hook method to add extra keys to self.state when first loaded.

        The first time this Crawler is used, or when the code has been
        upgraded, the saved state file may not contain all the keys you
        expect. Use this method to add any missing keys. Simply modify
        self.state as needed.

        This method for subclasses to override. No upcall is necessary.
        """
        pass

    def save_state(self):
        lcpi = self.last_complete_prefix_index
        if lcpi == -1:
            last_complete_prefix = None
        else:
            last_complete_prefix = self.prefixes[lcpi]
        self.state["last-complete-prefix"] = last_complete_prefix
        self._state_serializer.save(self.get_state())

    def startService(self):
        # arrange things to look like we were just sleeping, so
        # status/progress values work correctly
        self.sleeping_between_cycles = True
        self.current_sleep_time = self.slow_start
        self.next_wake_time = time.time() + self.slow_start
        self.timer = reactor.callLater(self.slow_start, self.start_slice)
        service.MultiService.startService(self)

    def stopService(self):
        if self.timer:
            self.timer.cancel()
            self.timer = None
        self.save_state()
        return service.MultiService.stopService(self)

    def start_slice(self):
        start_slice = time.time()
        self.timer = None
        self.sleeping_between_cycles = False
        self.current_sleep_time = None
        self.next_wake_time = None
        try:
            self.start_current_prefix(start_slice)
            finished_cycle = True
        except TimeSliceExceeded:
            finished_cycle = False
        self.save_state()
        if not self.running:
            # someone might have used stopService() to shut us down
            return
        # either we finished a whole cycle, or we ran out of time
        now = time.time()
        this_slice = now - start_slice
        # this_slice/(this_slice+sleep_time) = percentage
        # this_slice/percentage = this_slice+sleep_time
        # sleep_time = (this_slice/percentage) - this_slice
        sleep_time = (this_slice / self.allowed_cpu_percentage) - this_slice
        # if the math gets weird, or a timequake happens, don't sleep
        # forever. Note that this means that, while a cycle is running, we
        # will process at least one bucket every 5 minutes, no matter how
        # long that bucket takes.
        sleep_time = max(0.0, min(sleep_time, 299))
        if finished_cycle:
            # how long should we sleep between cycles? Don't run faster than
            # allowed_cpu_percentage says, but also run faster than
            # minimum_cycle_time
            self.sleeping_between_cycles = True
            sleep_time = max(sleep_time, self.minimum_cycle_time)
        else:
            self.sleeping_between_cycles = False
        self.current_sleep_time = sleep_time # for status page
        self.next_wake_time = now + sleep_time
        self.yielding(sleep_time)
        self.timer = reactor.callLater(sleep_time, self.start_slice)

    def start_current_prefix(self, start_slice):
        state = self.state
        if state["current-cycle"] is None:
            self.last_cycle_started_time = time.time()
            state["current-cycle-start-time"] = self.last_cycle_started_time
            if state["last-cycle-finished"] is None:
                state["current-cycle"] = 0
            else:
                state["current-cycle"] = state["last-cycle-finished"] + 1
            self.started_cycle(state["current-cycle"])
        cycle = state["current-cycle"]

        for i in range(self.last_complete_prefix_index+1, len(self.prefixes)):
            # if we want to yield earlier, just raise TimeSliceExceeded()
            prefix = self.prefixes[i]
            prefixdir = os.path.join(self.sharedir, prefix)
            if i == self.bucket_cache[0]:
                buckets = self.bucket_cache[1]
            else:
                try:
                    buckets = os.listdir(prefixdir)
                    buckets.sort()
                except EnvironmentError:
                    buckets = []
                self.bucket_cache = (i, buckets)
            self.process_prefixdir(cycle, prefix, prefixdir,
                                   buckets, start_slice)
            self.last_complete_prefix_index = i

            now = time.time()
            if self.last_prefix_finished_time is not None:
                elapsed = now - self.last_prefix_finished_time
                self.last_prefix_elapsed_time = elapsed
            self.last_prefix_finished_time = now

            self.finished_prefix(cycle, prefix)
            if time.time() >= start_slice + self.cpu_slice:
                raise TimeSliceExceeded()

        # yay! we finished the whole cycle
        self.last_complete_prefix_index = -1
        self.last_prefix_finished_time = None # don't include the sleep
        now = time.time()
        if self.last_cycle_started_time is not None:
            self.last_cycle_elapsed_time = now - self.last_cycle_started_time
        state["last-complete-bucket"] = None
        state["last-cycle-finished"] = cycle
        state["current-cycle"] = None
        self.finished_cycle(cycle)
        self.save_state()

    def process_prefixdir(self, cycle, prefix, prefixdir, buckets, start_slice):
        """This gets a list of bucket names (i.e. storage index strings,
        base32-encoded) in sorted order.

        You can override this if your crawler doesn't care about the actual
        shares, for example a crawler which merely keeps track of how many
        buckets are being managed by this server.

        Subclasses which *do* care about actual bucket should leave this
        method along, and implement process_bucket() instead.
        """

        for bucket in buckets:
            last_complete = self.state["last-complete-bucket"]
            if last_complete is not None and bucket <= last_complete:
                continue
            self.process_bucket(cycle, prefix, prefixdir, bucket)
            self.state["last-complete-bucket"] = bucket
            if time.time() >= start_slice + self.cpu_slice:
                raise TimeSliceExceeded()

    # the remaining methods are explictly for subclasses to implement.

    def started_cycle(self, cycle):
        """Notify a subclass that the crawler is about to start a cycle.

        This method is for subclasses to override. No upcall is necessary.
        """
        pass

    def process_bucket(self, cycle, prefix, prefixdir, storage_index_b32):
        """Examine a single bucket. Subclasses should do whatever they want
        to do to the shares therein, then update self.state as necessary.

        If the crawler is never interrupted by SIGKILL, this method will be
        called exactly once per share (per cycle). If it *is* interrupted,
        then the next time the node is started, some amount of work will be
        duplicated, according to when self.save_state() was last called. By
        default, save_state() is called at the end of each timeslice, and
        after finished_cycle() returns, and when stopService() is called.

        To reduce the chance of duplicate work (i.e. to avoid adding multiple
        records to a database), you can call save_state() at the end of your
        process_bucket() method. This will reduce the maximum duplicated work
        to one bucket per SIGKILL. It will also add overhead, probably 1-20ms
        per bucket (and some disk writes), which will count against your
        allowed_cpu_percentage, and which may be considerable if
        process_bucket() runs quickly.

        This method is for subclasses to override. No upcall is necessary.
        """
        pass

    def finished_prefix(self, cycle, prefix):
        """Notify a subclass that the crawler has just finished processing a
        prefix directory (all buckets with the same two-character/10bit
        prefix). To impose a limit on how much work might be duplicated by a
        SIGKILL that occurs during a timeslice, you can call
        self.save_state() here, but be aware that it may represent a
        significant performance hit.

        This method is for subclasses to override. No upcall is necessary.
        """
        pass

    def finished_cycle(self, cycle):
        """Notify subclass that a cycle (one complete traversal of all
        prefixdirs) has just finished. 'cycle' is the number of the cycle
        that just finished. This method should perform summary work and
        update self.state to publish information to status displays.

        One-shot crawlers, such as those used to upgrade shares to a new
        format or populate a database for the first time, can call
        self.stopService() (or more likely self.disownServiceParent()) to
        prevent it from running a second time. Don't forget to set some
        persistent state so that the upgrader won't be run again the next
        time the node is started.

        This method is for subclasses to override. No upcall is necessary.
        """
        pass

    def yielding(self, sleep_time):
        """The crawler is about to sleep for 'sleep_time' seconds. This
        method is mostly for the convenience of unit tests.

        This method is for subclasses to override. No upcall is necessary.
        """
        pass

class BucketCountingCrawler(ShareCrawler):
    """I keep track of how many buckets are being managed by this server.
    This is equivalent to the number of distributed files and directories for
    which I am providing storage. The actual number of files+directories in
    the full grid is probably higher (especially when there are more servers
    than 'N', the number of generated shares), because some files+directories
    will have shares on other servers instead of me. Also note that the
    number of buckets will differ from the number of shares in small grids,
    when more than one share is placed on a single server.
    """

    minimum_cycle_time = 60*60 # we don't need this more than once an hour

    def __init__(self, server, statefile, num_sample_prefixes=1):
        ShareCrawler.__init__(self, server, statefile)
        self.num_sample_prefixes = num_sample_prefixes

    def add_initial_state(self):
        # ["bucket-counts"][cyclenum][prefix] = number
        # ["last-complete-cycle"] = cyclenum # maintained by base class
        # ["last-complete-bucket-count"] = number
        # ["storage-index-samples"][prefix] = (cyclenum,
        #                                      list of SI strings (base32))
        self.state.setdefault("bucket-counts", {})
        self.state.setdefault("last-complete-bucket-count", None)
        self.state.setdefault("storage-index-samples", {})

    def process_prefixdir(self, cycle, prefix, prefixdir, buckets, start_slice):
        # we override process_prefixdir() because we don't want to look at
        # the individual buckets. We'll save state after each one. On my
        # laptop, a mostly-empty storage server can process about 70
        # prefixdirs in a 1.0s slice.
        if cycle not in self.state["bucket-counts"]:
            self.state["bucket-counts"][cycle] = {}
        self.state["bucket-counts"][cycle][prefix] = len(buckets)
        if prefix in self.prefixes[:self.num_sample_prefixes]:
            self.state["storage-index-samples"][prefix] = (cycle, buckets)

    def finished_cycle(self, cycle):
        last_counts = self.state["bucket-counts"].get(cycle, [])
        if len(last_counts) == len(self.prefixes):
            # great, we have a whole cycle.
            num_buckets = sum(last_counts.values())
            self.state["last-complete-bucket-count"] = num_buckets
            # get rid of old counts
            for old_cycle in list(self.state["bucket-counts"].keys()):
                if old_cycle != cycle:
                    del self.state["bucket-counts"][old_cycle]
        # get rid of old samples too
        for prefix in list(self.state["storage-index-samples"].keys()):
            old_cycle,buckets = self.state["storage-index-samples"][prefix]
            if old_cycle != cycle:
                del self.state["storage-index-samples"][prefix]


# Name: William Kreiser
# Date: 24 Sep 18
# Project: Lab3A

"""
sing the struct package from the python library, pack the values (1, 2, -3, -4)
as the following data types (unsigned short, unsigned int, signed short, signed int)

1 as an unsigned short
2 as an unsigned int
-3 as a signed short
-4 as a signed int
Write a TCP client that packs those values, sends the packed string to a server.

Write a TCP server that receives the string, unpacks it using little endian and prints it,
then unpacks it again using big endian and prints it.
"""
import struct
from socket import *

packed_data =  struct.pack('=HIhi', 1, 2, -3, -4)
s = socket(AF_INET, SOCK_STREAM)
s.connect(("localhost", 15000))

s.send(packed_data)

s.close()


#coding=utf-8

import xlrd
import jieba
from wordcloud import WordCloud, ImageColorGenerator, STOPWORDS
from scipy.misc import imread
import matplotlib.pyplot as plt

def getData():
    university ="清华大学	北京大学	厦门大学 南京大学	复旦大学	天津大学 浙江大学	南开大学	西安交通大学 东南大学	武汉大学	上海交通大学 山东大学	湖南大学	中国人民大学 吉林大学	重庆大学	电子科技大学 四川大学	中山大学	华南理工大学 兰州大学	东北大学	西北工业大学 哈尔滨工业大学	华中科技大学	中国海洋大学 北京理工大学	大连理工大学	北京航空航天大学 北京师范大学	同济大学	中南大学 中国科学技术大学 中国农业大学	国防科学技术大学	中央民族大学 华东师范大学	西北农林科技大学 北京邮电大学 上海财经大学 西南财经大学 中国政法大学 中央财经大学"
    # university = ""
    data = xlrd.open_workbook('2015年部属高校国家级大学生创新创业训练计划项目名单.xls')
    table = data.sheets()[0]
    nrows = table.nrows
    ncols = table.ncols
    project_num_dic = {}
    people_num_dic = {}
    avg_num_list = []
    project_num_list = []
    people_num_list = []
    university_list = []
    project_names_str = ""
    cost_total = 0
    ministry_cost = 0
    university_cost = 0
    university_cost_list = []
    university_cost_dic = {}
    subject_dic = {}
    teacher_dic = {}
    for i in range(3, nrows):
        row = table.row_values(i)
        print(row[14])
        print(row[11])

        if row[14] in subject_dic:
            subject_dic[row[14]] += 1
        else:
            subject_dic[row[14]] = 1

        if row[10] in teacher_dic:
            teacher_dic[row[10]] += 1
        else:
            teacher_dic[row[10]] = 1

        # project_names_str += row[4]
        # print(row[12])
        # cost_total += int(row[11])
        # ministry_cost += int(row[12])
        # university_cost += int(row[13])
        # if university.find(row[1]) == -1:
        #     if row[1] in university_cost_dic:
        #         university_cost_dic[row[1]] += int(row[11])
        #     else:
        #         university_cost_dic[row[1]] = int(row[11])
        # if university.find(row[1]) != -1:
        #     if row[1] in project_num_dic:
        #         project_num_dic[row[1]] += 1
        #         people_num_dic[row[1]] += int(row[7])
        #     else:
        #         print(row[7])
        #         project_num_dic[row[1]] = 1
        #         people_num_dic[row[1]] = int(row[7])
        # print(table.row_values(i))
        # print(table.col_values(i))
    data2 = xlrd.open_workbook('2016年部属高校国家级大学生创新创业训练计划项目名单.xlsx')
    table = data2.sheets()[1]
    nrows = table.nrows
    for i in range(2, nrows):
        row = table.row_values(i)
        # project_names_str += row[4]
        # print(row[12])
        if row[12] in teacher_dic:
            teacher_dic[row[12]] += 1
        else:
            teacher_dic[row[12]] = 1

        cost_total += int(row[15]) if row[15] != "" else 0
        ministry_cost += int(row[14]) if row[14] != "" else 0
        university_cost += int(row[13]) if row[13] != "" else 0
        if university.find(row[3]) == -1:
            if row[3] in university_cost_dic:
                university_cost_dic[row[3]] += int(row[15])
            else:
                university_cost_dic[row[3]] = int(row[15])

    for item in university_cost_dic.items():
        university_cost_list.append(list(item))
    university_cost_list.sort(key=lambda x:x[1], reverse=True)
    university_cost_list = university_cost_list[0:40]
    university_cost_list.sort(key=lambda x: x[1], reverse=False)
    print(university_cost_list)

    teacher_list = []
    other_teacher = 0
    for item in teacher_dic.items():
        teacher_list.append(list(item))
    for i in range(len(teacher_list)):
        if teacher_list[i][1] < 161:
            other_teacher += 1

    teacher_list.sort(key=lambda x: x[1], reverse=True)
    teacher_list = teacher_list[0:10]
    teacher_list.append(['其它',other_teacher])
    teacher_list.sort(key=lambda x: x[1], reverse=False)

    subject_list = []

    for item in subject_dic.items():
        subject_list.append(list(item))
    subject_list.sort(key=lambda x: x[1], reverse=False)
    print(subject_list)
    other_subject = 0
    for i in range(len(subject_list)):
        if subject_list[i][1] < 318:
            other_subject += 1

    subject_list.sort(key=lambda x: x[1], reverse=True)
    subject_list = subject_list[0:10]
    subject_list.append(['其它', other_subject])
    subject_list.sort(key=lambda x: x[1], reverse=False)
    # for key in project_num_dic.keys():
    #     temp = round(people_num_dic[key] / project_num_dic[key], 2)
    #     avg_num_list.append([key, temp])
    #
    # for item in project_num_dic.items():
    #     project_num_list.append(list(item))
    # for item in people_num_dic.items():
    #     people_num_list.append(list(item))
    #
    # for i in range (len(project_num_list)):
    #     university_list.append([project_num_list[i][0], project_num_list[i][1], people_num_list[i][1], avg_num_list[i][1]])
    #
    print([cost_total, ministry_cost, university_cost])
    print(teacher_list)
    print(subject_list)
    # word_text = splitWords(project_names_str)
    # drawWordCloud(word_text, 'project_name.png')
    # university_list.sort(key=lambda x:x[3], reverse=True)
    # print(university_list)
    # # print(project_num_list)
    # print(project_num_list)
    # print(people_num_list)
    # print(avg_num_list)
    # print(sorted(project_num_dic.items(), key= lambda item: item[1], reverse=True))
    # print(sorted(people_num_dic.items(), key= lambda item: item[1], reverse=True))
    # print(sorted(avg_num_dic.items(), key=lambda item: item[1], reverse=False))

def getData2016():
    university = "清华大学	北京大学	厦门大学 南京大学	复旦大学	天津大学 浙江大学	南开大学	西安交通大学 东南大学	武汉大学	上海交通大学 山东大学	湖南大学	中国人民大学 吉林大学	重庆大学	电子科技大学 四川大学	中山大学	华南理工大学 兰州大学	东北大学	西北工业大学 哈尔滨工业大学	华中科技大学	中国海洋大学 北京理工大学	大连理工大学	北京航空航天大学 北京师范大学	同济大学	中南大学 中国科学技术大学 中国农业大学	国防科学技术大学	中央民族大学 华东师范大学	西北农林科技大学 北京邮电大学 上海财经大学 西南财经大学 中国政法大学 中央财经大学"
    data = xlrd.open_workbook('2016年部属高校国家级大学生创新创业训练计划项目名单.xlsx')
    table = data.sheets()[1]
    nrows = table.nrows
    ncols = table.ncols
    project_num_dic = {}
    people_num_dic = {}
    avg_num_list = []
    project_num_list = []
    people_num_list = []
    university_list = []
    project_names_str = ""
    for i in range(3, nrows):
        row = table.row_values(i)
        print(row)
        print("row5" + str(row[5]))
        project_names_str += row[5]
        if university.find(row[3]) != -1:
            if row[1] in project_num_dic:
                project_num_dic[row[3]] += 1
                people_num_dic[row[3]] += int(row[9])
            else:
                print(row[9])
                project_num_dic[row[3]] = 1
                people_num_dic[row[3]] = int(row[9])
                # print(table.row_values(i))
                # print(table.col_values(i))
    for key in project_num_dic.keys():
        temp = round(people_num_dic[key] / project_num_dic[key], 2)
        avg_num_list.append([key, temp])

    for item in project_num_dic.items():
        project_num_list.append(list(item))
    for item in people_num_dic.items():
        people_num_list.append(list(item))

    for i in range(len(project_num_list)):
        university_list.append(
            [project_num_list[i][0], project_num_list[i][1], people_num_list[i][1], avg_num_list[i][1]])

    word_text = splitWords(project_names_str)
    drawWordCloud(word_text, 'project_name.png')

def splitWords(word):
    print('begin')
    word_list = jieba.cut(word, cut_all=False)
    word_list2 = []
    # 过滤掉大量重复的无意义的字段
    waste_word_str = "基于 研究 技术 方法 理论 为例 实验 影响 模拟 作用 应用 工程 探究 探索 浅析 机制 坚定 分析 调研 构建 特征 设计 方案 新型 及其 系统 公司 组织 平台 用于 对策 不同 使用 调查 合作 辅助 我国 地区"
    for word in word_list:
        # print(word)
        if len(word) >= 2 and waste_word_str.find(word) == -1:
            print(word)
            word_list2.append(word)
    # print(word_list2)
    word_text = " ".join(word_list2)
    # print(word_text)
    return word_text

def drawWordCloud(word_text, filename):
    mask = imread('pic.png')
    my_wordcloud = WordCloud(
        background_color='white',  # 设置背景颜色
        mask=mask,  # 设置背景图片
        max_words=2000,  # 设置最大现实的字数
        stopwords=STOPWORDS,  # 设置停用词
        font_path='/System/Library/Fonts/Hiragino Sans GB W6.ttc',  # 设置字体格式，如不设置显示不了中文
        max_font_size=50,  # 设置字体最大值
        random_state=30,  # 设置有多少种随机生成状态，即有多少种配色方案
        scale=1.5
    ).generate(word_text)
    image_colors = ImageColorGenerator(mask)
    my_wordcloud.recolor(color_func=image_colors)
    # 以下代码显示图片
    plt.imshow(my_wordcloud)
    plt.axis("off")
    plt.show()
    # 保存图片
    my_wordcloud.to_file(filename=filename)
    print("finish")

if __name__ == '__main__':
    getData()


import gym
# from gym import spaces  # When we use MultiDiscrete the action space
import numpy as np
from scipy.misc import imresize

class RacingGym:
	def __init__(self, env_name='CarRacing-v0', skip_actions=6, num_frames=4, w=42, h=42):
		# env_name: the name of the Open AI Gym environment. By default, CarRacing-v0
		# skip_actions: the number of frames to repeat an action for
		# num_frames: the number of frames to stack in one state
		# w: width of the state input
		# h: height of the state input.
		self.env = gym.make(env_name)
		self.num_frames = num_frames
		self.skip_actions = skip_actions
		self.w = w
		self.h = h

		# Limit the brake, as per explained in
		# https://github.com/oguzelibol/CarRacingA3C, but we also add 'no action':

		# No action, turn right, turn left, accelerate, brake, respectively.
		if env_name == 'CarRacing-v0':
			self.action_space = [[0, 0, 0], [1, 0, 0], [-1, 0, 0], [0, 1, 0], [0, 0, 0.8]]

		# In the future, try all the possible (discrete)  actions with MultiDiscrete action space:
		# self.action_space = gym.spaces.MultiDiscrete([[-1, 1], [0,1], [0,1]])

		else:
			self.action_space = range(self.env.action_space.n)

		# For the MultiDiscrete case, we can't use any in-built functions, so:

		# self.action_size = 12

		self.action_size = len(self.action_space)

		self.state = None
		self.env_name = env_name

	# Preprocess the input and stack the frames.
	def preprocess(self, obs, is_start=False):
		# First convert to grayscale
		grayscale = obs.astype('float32').mean(2)
		# Resize the image to w x h and scale to between 0 and 1
		s = imresize(grayscale, (self.w, self.h)).astype('float32')*(1.0/255.0)
		# Next reshape the image to a 4D array with 1st and 4th dimensions of
		# size 1
		s = s.reshape(1, s.shape[0], s.shape[1], 1)
		# Now stack the frames. If this is the first frame, then repeat it
		# num_frames times.
		if is_start or self.state is None:
			self.state = np.repeat(s, self.num_frames, axis = 3)
		else:
			self.state = np.append(s, self.state[:, :, :, :self.num_frames-1], axis = 3)
		return self.state

	# Render the current frame
	def render(self):
		self.env.render()

	# Reset the environment and return the state.
	def reset(self):
		return self.preprocess(self.env.reset(), is_start = True)

	# Step the environment with the given action
	def step(self, action_idx):
		action = self.action_space[action_idx]
		accum_reward = 0
		prev_s = None
		for _ in range(self.skip_actions):
			s, r, term, info = self.env.step(action)
			accum_reward += r
			if term:
				break
			prev_s = s

		if prev_s is not None:
			s = np.maximum.reduce([s, prev_s])
		return self.preprocess(s), accum_reward, term, info


#!/usr/bin/env python

"""The setup script."""

from setuptools import setup, find_packages

requirements = ['Click>=7.0', ]

setup_requirements = [ ]

test_requirements = [ ]

setup(
    author="Abijith B",
    author_email='abijithbahuleyan@gmail.com',
    python_requires='>=3.5',
    classifiers=[
        'Development Status :: 2 - Pre-Alpha',
        'Intended Audience :: Developers',
        'License :: OSI Approved :: MIT License',
        'Natural Language :: English',
        'Programming Language :: Python :: 3',
        'Programming Language :: Python :: 3.5',
        'Programming Language :: Python :: 3.6',
        'Programming Language :: Python :: 3.7',
        'Programming Language :: Python :: 3.8',
    ],
    description="Compress image using k-means algorithm.",
    entry_points={
        'console_scripts': [
            'kompressor=kompressor.cli:main',
        ],
    },
    install_requires=requirements,
    license="MIT license",
    long_description= "",
    include_package_data=True,
    keywords='kompressor',
    name='kompressor',
    packages=find_packages(include=['kompressor', 'kompressor.*']),
    setup_requires=setup_requirements,
    test_suite='tests',
    tests_require=test_requirements,
    url='https://github.com/twentyse7en/kompressor',
    version='0.1.0',
    zip_safe=False,
)


from tkinter import *
from tkinter.ttk import *
import tkinter

windows = Tk()
windows.title("Hello")
windows.geometry("1000x300") #kích thước của cửa sổ
windows.configure(background = "pink")
#thêm tiêu đề:

lbl = tkinter.Label(windows, text="Phép tính  A và B", background = "pink", fg="BLACK", font=("Arial", 30))
lbl.grid(column=0, row=0)

#thêm đối tượng

combo = Combobox(windows)
lblcombo = tkinter.Label(windows, text="Phép tính:",background = "pink", fg="BLACK", font=("Arial", 15))
lblcombo.grid(column=2, row=2)
combo['values'] = ("Tính Tổng", "Tính Hiệu", "Tính Tích", "Tính Thương", "Hàm bậc nhất: ax+b=0")
combo.current(0)
combo.grid(column=3, row=2)

txta = Entry(windows, width=15)
txta.grid(column=2, row=3)

lbla = tkinter.Label(windows, text="Nhập A:",background = "yellow", fg="BLACK", font=("Arial", 15))
lbla.grid(column=1, row=3)

txtb = Entry(windows, width=15)
txtb.grid(column=4, row=3)

lblb = tkinter.Label(windows, text="Nhập B:",background = "yellow", fg="BLACK", font=("Arial", 15))
lblb.grid(column=3, row=3)

output = tkinter.Label(windows, text="Kết quả: ",background = "red", fg="BLACK", font=("Arial", 20))
output.grid(column=3, row=6)

def handleButton():
	pheptinh=str(combo.get())
	if pheptinh=="Tính Tổng":
		a=int(txta.get())
		b=int(txtb.get())
		c=a+b
		print(c)
	if pheptinh=="Tính Hiệu":
		a=int(txta.get())
		b=int(txtb.get())
		c=a-b
	if pheptinh=="Tính Tích":
		a=int(txta.get())
		b=int(txtb.get())
		c=a*b
	if pheptinh=="Tính Thương":
		a=int(txta.get())
		b=int(txtb.get())
		if b==0:
			c="PTVN"
		else:
			c=round(a/b, 2)
	if pheptinh=="Hàm bậc nhất: ax+b=0":
		a=int(txta.get())
		b=int(txtb.get())
		if a==0:
			c="PTVN"
		else:
			c=round(-b/a, 2)
	output['text'] = "Kết quả: " + str(c)
#thêm nút

btnHello = tkinter.Button(windows, text="TÍNH", fg="BLACK", font=("Arial", 20), command=handleButton)
btnHello.grid(column=3, row=4)

windows.mainloop()

#in ra kết quả


import datetime

ano = int(input("Ingresa el ano: "))
mes = int(input("Ingresa el mes: "))
dia = int(input("Ingresa el dia: "))
fecha_ingresada = datetime.datetime(ano, mes, dia)
hoy = datetime.datetime.now()

horas_faltantes = (hoy - fecha_ingresada)
if horas_faltantes.total_seconds() > 0:
    print("Han pasado {} horas".format(int(horas_faltantes.total_seconds()/3600)))
else:
    print("Faltan {} horas".format(abs(int(horas_faltantes.total_seconds() / 3600))))


# 퍼셉트론 : 다수의 신호(0 or 1)를 입력받아 하나의 신호를 출력
# 가중치가 클수록 해당 신호가 중요함을 나타냄
# 신경망의 기원
# 가중치를 설정하는 작업을 사람이
# 신경망 : 가중치의 적절한 값을 데이터로부터 자동으로 학습

# and 게이트
import numpy as np

def AND(x1, x2):
    x = np.array([x1, x2])
    w = np.array([0.5, 0.5])
    b = -0.5
    temp = np.dot(w, x) + b
    print(temp)
    if temp <= 0:
        return 0
    else:
        return 1

def OR(x1, x2):
    x = np.array([x1, x2])
    w = np.array([0.5, 0.5])
    b = -0.3
    temp = np.dot(w, x) + b
    # print(temp)
    if temp <= 0:
        return 0
    else:
        return 1

def NAND(x1, x2):
    x = np.array([x1, x2])
    w = np.array([-0.5, -0.5])
    b = 0.7
    temp = np.dot(w, x) + b
    # print(temp)
    if temp <= 0:
        return 0
    else:
        return 1

def XOR(x1, x2):
    r1 = NAND(x1, x2)
    r2 = OR(x1, x2)
    y = AND(r1, r2)
    return y

if __name__ == '__main__':
    for xx in [(0, 0), (0, 1), (1, 0), (1, 1)]:
        # y = AND(xx[0], xx[1])
        # y = OR(xx[0], xx[1])
        # y = NAND(xx[0], xx[1])
        y = XOR(xx[0], xx[1])
        # print(xx, '-->', y)

import matplotlib.pyplot as plt

def stepFunction(x):
    return np.array(x > 0, np.int)

def sigmoid(x):
    return 1/(1+np.exp(-x))

x = np.arange(-5, 5, 0.1)
print(x)
y = stepFunction(x)
y2 = sigmoid(x)
plt.plot(x, y)
plt.plot(x, y2)
plt.show()


import math
import random

import numpy as np

def ransac(measurements, N=25, S=5, X=25, C=15):
    filtered = []
    for measurement in measurements:
        if measurement[1] == 0:
            filtered.append(False)
        else:
            filtered.append(True)

    measurements = measurements[filtered]

    cartesian = np.array([ptc(xi) for xi in measurements])
    lines1, segs1, ret = _ransac(cartesian, N, S, X, C)

    return lines1, segs1, ret

def _ransac(cartesian, N=50, S=7, X=10, C=15):
    lines = []
    line_segs = []

    ret = None

    f = True

    for _ in range(N):
        size = len(cartesian)
        if size <= 2 * S - 1:
            return lines, line_segs, ret
        indexes = [*range(S)]
        indexes.remove(S // 2)
        indexes = np.array(indexes)

        start = random.randint(S, size - S)
        np.random.shuffle(indexes)
        translated = indexes + np.ones(S - 1) * start
        sample_data = np.array([cartesian[int(i)] for i in translated])
        sample_data.put(0, cartesian[start])

        lsrl = np.linalg.lstsq(
            np.vstack([sample_data.T[0],
                       np.ones(len(sample_data.T[0]))]).T,
            sample_data.T[1], rcond=None)[0]

        normal = np.array([1, lsrl[0]])
        normal = normal / np.linalg.norm(normal)
        inter = np.array([0, lsrl[1]])
        filtered = cartesian[abs(np.cross(cartesian - inter, normal)) < X]

        groups = [[]]
        o = None
        for i in filtered:
            if o is None:
                o = i
                continue

            if np.linalg.norm(o - i) >= X:
                groups.append([])

            o = i

            groups[len(groups) - 1].append(i)

        max_l = 0
        index = 0
        for i, g in enumerate(groups):
            if len(g) > max_l:
                max_l = len(g)
                index = i

        if len(groups[index]) > C:
            filtered = np.array(groups[index])
            new_lsrl = np.linalg.lstsq(
                    np.vstack([filtered.T[0],
                               np.ones(len(filtered.T[0]))]).T,
                    filtered.T[1], rcond=None)[0]

            lines.append(new_lsrl)
            line_segs.append(np.array([filtered[0], filtered[len(filtered) - 1]]))

            if f:
                f = False
                ret = [
                    [cartesian[start]],
                    sample_data,
                    filtered,
                    new_lsrl
                ]

            filtered = np.logical_not(np.isin(cartesian, filtered).T[0])
            cartesian = cartesian[filtered]

    return lines, line_segs, ret

def ptc(coords):
    return np.array([coords[1] * math.cos(coords[0]), coords[1] * math.sin(coords[0])])


import os

if __name__ == '__main__':

    input_dir = 'submissions/submission4'
    sub_name = input_dir.split('/')[-1]
    out_dir = f'submissions/{sub_name}_per_carac'
    if not os.path.exists(out_dir):
        os.makedirs(out_dir)
    n_img = len(os.listdir(input_dir))
    for i, dirname in enumerate(os.listdir(input_dir)):
        dirpath = os.path.join(input_dir, dirname)
        for fname in os.listdir(dirpath):
            carac = fname.split('.')[0]
            if not os.path.exists(os.path.join(out_dir, carac)):
                os.mkdir(os.path.join(out_dir, carac))
            src_path = os.path.join(dirpath, fname)
            target_path = os.path.join(out_dir, carac, dirname + '.png')
            os.system(f'cp {src_path} {target_path}')
        print(f'image {i+1}/{n_img} moved  ', end='\r')
    print()


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

import traceback
from importlib import reload
from unittest import TestCase
from unittest.mock import Mock, call, patch

import falcon

from smapy import api, middleware
from smapy.actions import hello

class TestExceptionSerializer(TestCase):

    def test_exception_serializer(self):
        """Just verify that to_dict is called and result assigned to body"""
        resp = Mock()
        exception = Mock()
        exception.to_dict.return_value = {'an': 'exception'}
        api.exception_serializer(None, resp, exception)

        self.assertEqual({'an': 'exception'}, resp.body)

class TestUnknownExceptionSerializer(TestCase):

    def test_unknown_exception_serializer_httperror(self):
        """If exception is an HTTPError raise it without doing anything else."""

        original = None
        with self.assertRaises(falcon.HTTPError) as raised:
            try:
                # We need to actually raise it to have a valid context.
                raise falcon.HTTPError(404)

            except falcon.HTTPError as ex:
                original = ex
                api.unknown_exception_serializer(ex, None, None, None)

        self.assertEqual(original, raised.exception)

    def test_unknown_exception_serializer_other(self):
        """If exception is NOT an HTTPError raise an HTTPInternalServerError."""

        tb = None
        with self.assertRaises(falcon.HTTPInternalServerError) as raised:
            try:
                # We need to actually raise it to have a valid context.
                raise Exception('a message')

            except Exception as ex:
                tb = traceback.format_exc().splitlines()[-3:]
                api.unknown_exception_serializer(ex, None, None, None)

        exception = raised.exception
        self.assertEqual('Exception: a message', exception.title)
        self.assertEqual(tb, exception.description)

class TestAPI(TestCase):

    def setUp(self):
        # Reload the api module to get rid of previous mocking
        reload(api)

    # #########################
    # _is_action(obj, module) #
    # #########################
    def test__is_action_not_a_class(self):
        """If object is not a class, it is not an Action."""
        obj = 'not a class'
        module = None

        is_action = api.API._is_action(obj, module)
        self.assertFalse(is_action)

    def test__is_action_imported(self):
        """If object was imported, it is not an Action."""
        # We use an imported module
        obj = api.MongoClient    # This is imported from pymongo inside api/__init__.py
        module = api

        is_action = api.API._is_action(obj, module)
        self.assertFalse(is_action)

    def test__is_action_not_an_action(self):
        """Obj is not a BaseAction subclass."""
        obj = api.API    # This is truly defined inside api/__init__.py, but it's not an Action
        module = api

        is_action = api.API._is_action(obj, module)
        self.assertFalse(is_action)

    def test__is_action_true(self):
        """Obj is really a BaseAction subclass."""
        obj = hello.World
        module = hello

        is_action = api.API._is_action(obj, module)
        self.assertTrue(is_action)

    # ###############################
    # _add_runnable(self, runnable) #
    # ###############################
    def test__add_runnable_duplicated(self):
        """If runnable is already registered, raise an exception."""

        # Override __init__
        api.API.__init__ = lambda x: None

        api_ = api.API()

        api_.runnables = {'a_runnable': Mock()}

        runnable = Mock()
        runnable.name = 'a_runnable'

        with self.assertRaises(ValueError) as ve:
            api_._add_runnable(runnable)

        exception = ve.exception
        self.assertEqual('Duplicated runnable name: a_runnable', str(exception))

    def test__add_runnable_ok(self):
        """If runnable is NOT registered, set it into runnables."""

        # Override __init__
        api.API.__init__ = lambda x: None

        api_ = api.API()

        api_.runnables = dict()

        runnable = Mock()
        runnable.name = 'a_runnable'

        api_._add_runnable(runnable)

        self.assertEqual({'a_runnable': runnable}, api_.runnables)

    # #############################
    # load_actions(self, modules) #
    # #############################
    # FIXME
    # @patch('smapy.api.find_submodules')
    # def test_load_actions(self, find_submodules_mock):
    #     """If a module attribute is an action, add it."""

    #     # Set up
    #     module = Mock()
    #     action = Mock()
    #     module.an_action = action

    #     find_submodules_mock.return_value = [module]

    #     # Override __init__
    #     api.API.__init__ = lambda x: None
    #     api_ = api.API()

    #     # Mock _is_action and _add_runnable
    #     def _is_action(obj, module):
    #         return obj == action

    #     api_._is_action = Mock(side_effect=_is_action)
    #     api_._add_runnable = Mock()

    #     # Actual call
    #     api_.load_actions('a.package')

    #     # Asserts
    #     find_submodules_mock.assert_called_once_with('a.package')
    #     api_._add_runnable.assert_called_once_with(action)

    # #####################################################
    # add_resource(self, route, resource_class, **kwargs) #
    # #####################################################
    def test_add_resource(self):
        """The resource must be initialized, added as a runnable and related to a route."""

        # Set up
        resource = Mock()
        api_ = Mock()
        api_.add_resource = api.API.add_resource.__get__(api_, api.API)

        # Actual call
        api_.add_resource('a_route', resource, keyword='argument')

        # Asserts
        api_._add_runnable.assert_called_once_with(resource, route='a_route', keyword='argument')
        api_.add_route.assert_called_once_with('a_route', resource)

    # #############################
    # _set_mongodb_up(self, conf) #
    # #############################
    @patch('smapy.api.MongoClient')
    def test__set_mongodb_up_no_audit(self, mongo_client_mock):
        """If audit is not defined, reuse self.mongodb."""

        # Set up
        mongo_client_mock.return_value = {
            'a_database': 'a_database',
            'audit_db': 'audit_db'    # this won't be used here
        }
        conf = {
            'mongodb': {
                'host': 'a_host',
                'port': 1234,
                'database': 'a_database'
            }
        }

        # Override __init__
        api.API.__init__ = lambda x: None
        api_ = api.API()

        # Actual call
        api_._set_mongodb_up(conf)

        # Asserts
        mongo_client_mock.assert_called_once_with(host='a_host', port=1234, connect=False)

        self.assertEqual('a_database', api_.mongodb)
        self.assertEqual('a_database', api_.auditdb)

    @patch('smapy.api.MongoClient')
    def test__set_mongodb_up_audit(self, mongo_client_mock):
        """If audit is defined, create a new client."""

        # Set up
        mongo_client_mock.return_value = {
            'a_database': 'a_database',
            'audit_db': 'audit_db'    # this WILL be used here
        }
        conf = {
            'mongodb': {
                'host': 'a_host',
                'port': 1234,
                'database': 'a_database'
            },
            'audit': {
                'host': 'audit_host',
                'port': 4321,
                'database': 'audit_db'
            }
        }

        # Override __init__
        api.API.__init__ = lambda x: None
        api_ = api.API()

        # Actual call
        api_._set_mongodb_up(conf)

        # Asserts
        calls = [
            call(host='a_host', port=1234, connect=False),
            call(host='audit_host', port=4321, connect=False)
        ]
        # self.assertEqual(calls, mongo_client_mock.call_args_list)
        assert calls == mongo_client_mock.call_args_list

        self.assertEqual('a_database', api_.mongodb)
        self.assertEqual('audit_db', api_.auditdb)

    # ######################
    # __init__(self, conf) #
    # ######################
    @patch('smapy.api.RemoteRunnable')
    def test___init__(self, remote_runnable_mock):
        """Make sure that everything is called with the right parameters."""

        # Set up
        conf = {
            'mongodb': 'mongodb',
            'audit': 'auditdb',
            'api': {
                'endpoint': 'an_endpoint',
                'default_resources': False
            }
        }

        # Mock _get_mongodb and _add_runnable
        def _get_mongodb(conf):
            return conf

        api.API._get_mongodb = Mock(side_effect=_get_mongodb)
        api.API.add_route = Mock()

        # Actual call
        api_ = api.API(conf)

        # Asserts
        _get_mongodb_calls = [
            call('mongodb'),
            call('auditdb')
        ]
        self.assertEqual(_get_mongodb_calls, api_._get_mongodb.call_args_list)

        # This is a bit hacky.
        # Here we go into the API._middleware list and look for the classes
        # which the registered methods belong to.
        self.assertEqual(2, len(api_._middleware[0]))
        self.assertIsInstance(api_._middleware[0][0][0].__self__, middleware.JSONSerializer)
        self.assertIsInstance(api_._middleware[0][1][0].__self__, middleware.ResponseBuilder)

        exception_serializer = api_._serialize_error
        self.assertEqual(api.exception_serializer, exception_serializer)

        unknown_exception_serializer = {e: f for e, f in api_._error_handlers}[Exception]
        self.assertEqual(
            api.unknown_exception_serializer.__name__,
            unknown_exception_serializer.__name__
        )

        self.assertEqual(api_.endpoint, 'an_endpoint')

        remote_runnable_mock.init.assert_called_once_with(api_)
        api_.add_route.assert_called_once_with(remote_runnable_mock.route, remote_runnable_mock)

        runnables = {
            'hello.World': hello.World
        }
        self.assertEqual(runnables, api_.runnables)


import numpy as np
import pandas as pd
import xlsxwriter
# import subprocess

persona = pd.read_csv("censo_2018.csv")
vivienda = pd.read_csv("vivienda_2018.csv")
hogar = pd.read_csv("hogares_2018.csv")
mgeonal = pd.read_csv("mgn_2018.csv")

# print(set(list(persona.columns)) & set(list(mgeonal.columns)))
persona = persona.merge(mgeonal, how = 'left',
						on = ['COD_ENCUESTAS',
							  'U_VIVIENDA', 'U_DPTO',
							  'U_MPIO', 'UA_CLASE'])

# print(set(list(vivienda.columns)) & set(list(mgeonal.columns)))
vivienda = vivienda.merge(mgeonal, how = 'left',
						  on = ['COD_ENCUESTAS',
								'U_VIVIENDA', 'U_DPTO',
								'U_MPIO', 'UA_CLASE'])

# print(set(list(hogar.columns)) & set(list(mgeonal.columns)))
hogar = hogar.merge(mgeonal, how = 'left',
					on = ['COD_ENCUESTAS',
						  'U_VIVIENDA', 'U_DPTO',
						  'U_MPIO', 'UA_CLASE'])

# persona.info()
# vivienda.info()
# hogar.info()
# mgeonal.info()

# Edad en Grupos Quinquenales
persona["EDAD"] = np.where(persona["P_EDADR"] == 0 , "Edad_De_00A04",
np.where(persona["P_EDADR"] == 1 ,"Edad_De_05A09",
np.where(persona["P_EDADR"] == 2 ,"Edad_De_10A14",
np.where(persona["P_EDADR"] == 3 ,"Edad_De_10A14",
np.where(persona["P_EDADR"] == 4 , "Edad_De_15A19",
np.where(persona["P_EDADR"] == 5 , "Edad_De_20A24",
np.where(persona["P_EDADR"] == 6 , "Edad_De_25A29",
np.where(persona["P_EDADR"] == 7 , "Edad_De_30A34",
np.where(persona["P_EDADR"] == 8 , "Edad_De_35A39",
np.where(persona["P_EDADR"] == 9 , "Edad_De_40A44",
np.where(persona["P_EDADR"] == 10 , "Edad_De_45A49",
np.where(persona["P_EDADR"] == 11 , "Edad_De_50A54",
np.where(persona["P_EDADR"] == 12 , "Edad_De_55A59",
np.where(persona["P_EDADR"] == 13 , "Edad_De_60A64",
np.where(persona["P_EDADR"] == 14 , "Edad_De_65A69",
np.where(persona["P_EDADR"] == 15 , "Edad_De_70A74",
np.where(persona["P_EDADR"] == 16 , "Edad_De_75A79",
np.where(persona["P_EDADR"] == 17 , "Edad_De_80A84",
np.where(persona["P_EDADR"] == 18 , "Edad_De_85A89",
np.where(persona["P_EDADR"] == 19 , "Edad_De_90A94",
np.where(persona["P_EDADR"] == 20 , "Edad_De_95A99",
np.where(persona["P_EDADR"] == 21 , "Edad_De_100_más", ""
))))))))))))))))))))))
# persona["EDAD"].value_counts()
# df = persona.groupby('P_EDADR')['COD_ENCUESTAS'].nunique()
# print(df)

# Alfabetismo
persona['ALFABETA'] = np.where(persona["P_ALFABETA"] == 1 , "Si_Alfabeta",
np.where(persona["P_ALFABETA"] == 2 ,"No_Alfabeta",
np.where(persona["P_ALFABETA"] == 9 ,"No_Informa_Alfabeta", "No_Aplica_Alfabeta")))
# persona["ALFABETA"].value_counts()

# Asistencia Escolar
persona['ESCOLAR'] = np.where(persona["PA_ASISTENCIA"] == 1 , "Si_Asiste",
np.where(persona["PA_ASISTENCIA"] == 2 ,"No_Asiste",
np.where(persona["PA_ASISTENCIA"] == 9 ,"No_Informa_Asiste", "No_Aplica_Asiste")))
# persona["ESCOLAR"].value_counts()

# Genero
persona['SEXO'] = np.where(persona["P_SEXO"] == 1 , "Hombre",
np.where(persona["P_SEXO"] == 2 ,"Mujer",""))
# persona["SEXO"].value_counts()

# Etnias
persona['ETNIA'] = np.where(persona["PA1_GRP_ETNIC"] == 1 , "Indigena",
np.where(persona["PA1_GRP_ETNIC"] == 2 ,"Gitano",
np.where(persona["PA1_GRP_ETNIC"] == 3 ,"Raizal",
np.where(persona["PA1_GRP_ETNIC"] == 4 ,"Palenquero",
np.where(persona["PA1_GRP_ETNIC"] == 5 ,"Negro_Afro",
np.where(persona["PA1_GRP_ETNIC"] == 6 ,"Ninguno",
np.where(persona["PA1_GRP_ETNIC"] == 9 ,"No_Informa", "")))))))
# persona["ETNIA"].value_counts()

# Generando tablas por Manzana
# pop_tot = (persona.groupby(['COD_DANE_ANM','P_EDADR'])['P_EDADR'].agg('count').reset_index(name='Count'))
pop_tot = (persona.groupby(['COD_DANE_ANM'])['COD_DANE_ANM'].agg('count').reset_index(name='Count'))
pop_tot.rename(columns={"Count": "Población"}, inplace=True)

pop_eda = pd.crosstab(index=persona['COD_DANE_ANM'], columns=persona['EDAD'])
pop_etn = pd.crosstab(index=persona['COD_DANE_ANM'], columns=persona['ETNIA'])
pop_alf = pd.crosstab(index=persona['COD_DANE_ANM'], columns=persona['ALFABETA'])
pop_gen = pd.crosstab(index=persona['COD_DANE_ANM'], columns=persona['SEXO'])
pop_asi = pd.crosstab(index=persona['COD_DANE_ANM'], columns=[persona['EDAD'], persona['ESCOLAR']])

# Generando tabla de poblacion por Manzana
pop_man = pop_tot.merge(pop_eda, how = 'outer', on = ['COD_DANE_ANM'])
pop_man = pop_man.merge(pop_etn, how = 'outer', on = ['COD_DANE_ANM'])
pop_man = pop_man.merge(pop_alf, how = 'outer', on = ['COD_DANE_ANM'])
pop_man = pop_man.merge(pop_gen, how = 'outer', on = ['COD_DANE_ANM'])
pop_man = pop_man.merge(pop_asi, how = 'outer', on = ['COD_DANE_ANM'])

# Generando tabla de hogares por Manzana
hog_tot = (hogar.groupby(['COD_DANE_ANM'])['COD_DANE_ANM'].agg('count').reset_index(name='Count'))
hog_tot.rename(columns={"Count": "Hogares"}, inplace=True)

# Generando tabla de hogares por Manzana
viv_tot = (vivienda.groupby(['COD_DANE_ANM'])['COD_DANE_ANM'].agg('count').reset_index(name='Count'))
viv_tot.rename(columns={"Count": "Viviendas"}, inplace=True)

# Informacion del IPM
ipm = pd.read_csv("imp_manzanas.csv")
# ipm.info()
ipm.drop(columns="Unnamed: 0", inplace=True)
ipm.rename(columns={"cod_dane": "COD_DANE_ANM"}, inplace=True)

# Base por manzanas
manzanas = pop_man.merge(hog_tot, how = 'outer', on = ['COD_DANE_ANM'])
manzanas = manzanas.merge(viv_tot, how = 'outer', on = ['COD_DANE_ANM'])
manzanas = manzanas.merge(ipm, how = 'left', on = ['COD_DANE_ANM'])

manzanas.to_csv('Censo_manzanas.csv', index=False)

manzanas = pd.read_csv("Censo_manzanas.csv")

dup = manzanas.pivot_table(index=['COD_DANE_ANM'], aggfunc='size')
len(dup)
len(manzanas)

codmz = pd.read_excel("codmz.xlsx", engine="openpyxl")
# codmz.set_index('OBJECTID',inplace=True)

mz = codmz.merge(manzanas, how='left', left_on='MANZ_CCNCT', right_on='COD_DANE_ANM')

mz.to_excel('manzanas.xlsx', index=False, engine='xlsxwriter')


from django.http import JsonResponse
from django.shortcuts import redirect, render
import os, datetime

from project.login import check_login

def index(request):
    check_login(request)
    context = {
        'msg' : 'success',
        'page' : 'main page.',
    }
    return render(request, 'cashbook/base.html', context)

def add(request):
    check_login(request)
    context = {
        'msg' : 'success',
        'page' : 'main page.',
    }
    return render(request, 'cashbook/add.html', context)


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

#####
# leba3207
####

from pdb import set_trace as dbg  # Utiliser dbg() pour faire un break dans votre code.

import numpy as np

class Correcteur:
    def __init__(self, p_init, p_transition, p_observation, int2letters, letters2int):
        """Correcteur de frappes dans un mot.

        Modèle de Markov caché (HMM) permettant de corriger les erreurs de frappes
        dans un mot. La correction est dictée par l'inférence de l'explication
        la plus pausible du modèle.

        Parameters
        ------------
        p_init : array-like shape (N,)
                 Probabilités initiales de l'état caché à la première position.

        p_transition : array-like shape (X,Y)
                       Modèle de transition.

        p_observation : array-like shape (X,Y)
                        Modèle d'observation.

        int2letters : list
                      Associe un entier (l'indice) à une lettre.

        letters2int : dict
                      Associe une lettre (clé) à un entier (valeur).
        """
        self.p_init = p_init
        self.p_transition = p_transition
        self.p_observation = p_observation
        self.int2letters = int2letters
        self.letters2int = letters2int

    def corrige(self, mot):
        """Corrige les frappes dans un mot.

        Retourne la correction du mot donné et la probabilité p(mot, mot corrigé).

        Parameters
        ------------
        mot : string
              Mot à corriger.

        Returns
        -----------
        mot_corrige : string
                      Le mot corrigé.

        prob : float
               Probabilité dans le HMM du mot observé et du mot corrigé.
               C'est-à-dire 'p(mot, mot_corrige)'.
        """

        T = len(mot)
        transitions, alpha = self.calcul_alpha(mot, T, self.p_init.size)
        best_prob, int_best_prob = self.get_best_proba(alpha, T)
        best_probs = self.get_best_probas(alpha, T)

        corrected_word = ''
        for b, i in best_probs:
            print(self.int2letters[int(i)])
            corrected_word += self.int2letters[int(i)]

        path = []
        val = (int_best_prob, T-1)
        for i in reversed(range(T)):
            for e in transitions:
                if e[1] == val:
                    path.append(e)
                    val = e[0]
        path.reverse()

        # word correction
        correct_word = list(mot)
        for p in range(T-1):
            correct_word[p] = path[p][0][0]
        correct_word[T-1] = path[T-2][1][0]  # last letter

        for f in range(T):
            correct_word[f] = self.int2letters[correct_word[f]]
        correct_word = ''.join(correct_word)

        return correct_word, best_prob

    def calcul_alpha(self, mot, T, nb_classes):
        alpha = np.zeros((T, nb_classes))
        transitions = []
        for t in range(T):
            letter = mot[t]
            letter_int = self.letters2int.get(letter)
            if t == 0:
                alpha[t] = self.p_init[letter_int] * self.p_observation[letter_int]
            else:
                for i in range(nb_classes):
                    p_transitions = np.zeros(nb_classes)
                    for j in range(nb_classes):
                        p_transitions[j] = self.p_transition[i, j] * alpha[t-1, j]
                    max_p_trans = np.amax(p_transitions)
                    class_max_p_trans = np.argmax(p_transitions)
                    alpha[t, i] = self.p_observation[letter_int, i] * max_p_trans
                    transitions.append(((class_max_p_trans, t-1), (i, t)))
        return transitions, alpha

    def get_best_proba(self, alpha, T):
        best_prob = np.amax(alpha[T-1])
        int_best_prob = np.argmax(alpha[T-1])
        return best_prob, int_best_prob

    def get_best_probas(self, alpha, T):
        best_prob = np.zeros((T, 2))
        for i in range(T):
            p = np.amax(alpha[i])
            int_p = np.argmax(alpha[i])
            best_prob[i] = [p, int_p]
        return best_prob


import json
import os
import ssl
import sys
from collections import OrderedDict
from pprint import pprint as pp

def main():
    debug = False
    if len(sys.argv) == 3:
      if sys.argv[2] == "-d":
        debug = True

    if debug:
      print("Python {:s} on {:s}\n".format(sys.version, sys.platform))
      print("cli arg1: {:s}\n".format(sys.argv[1]))

    cert_file_name = os.path.join(os.path.dirname(__file__), sys.argv[1])
    try:
        ordered_dict = OrderedDict()
        ordered_dict = ssl._ssl._test_decode_cert(cert_file_name)
        if debug: pp(ordered_dict)

    except Exception as e:
        print("Error decoding certificate: {:s}\n".format(e))

    print(json.dumps(ordered_dict))

if __name__ == "__main__":
    main()


from flask import Flask, redirect, request, jsonify
from spotifyapp.spotify import get_spotify_auth_url, get_new_releases

app = Flask(__name__)

app.config.update(
    SECRET_KEY='abc123',
    TEMPLATES_AUTO_RELOAD=True
)

# spotify sends the user here after clicking "agree" on sign-in page.
@app.route('/callback')
def show():
    # we need to grab the `code` variable from the URL to
    # use spotify's API later on.
    code = request.args.get('code')

    # calling our own function to fetch latest releases through
    # spotify API
    releases = get_new_releases("SE", 0, 20, code)

    # show the relases to the user in the browser
    return jsonify(releases)

# User goes here in our application when they want to sign in
@app.route('/auth')
def show_auth():
    # Calling our own function to generate a proper URL for authentication
    auth_url = get_spotify_auth_url()

    # Redirect the user to spotify's login page.
    return redirect(auth_url)


from PyQt5.QtWidgets import QWidget, QApplication
from PyQt5.QtGui import QPainter, QColor, QBrush
import sys

class Example(QWidget):
    def __init__(self):
        super().__init__()

        self.initUI()

    def initUI(self):
        self.setGeometry(300, 300, 350, 100)
        self.setWindowTitle('Colours')
        self.show()

    def paintEvent(self, e):
        qp = QPainter()
        qp.begin(self)
        self.drawRectangles(qp)
        qp.end()

    def drawRectangles(self, qp):
        col = QColor(0, 0, 0)
        # Here we define a colour using a hexadecimal notation.
        col.setNamedColor('#d4d4d4')
        # Here we define a brush and draw a rectangle.
        # A brush is an elementary graphics object
        # which is used to draw the background of a shape.
        # The drawRect() method accepts four parameters.
        qp.setPen(col)

        qp.setBrush(QColor(200, 0, 0))
        #  The method draws the rectangle using the current pen and brush.
        qp.drawRect(10, 15, 90, 60)

        qp.setBrush(QColor(255, 80, 0, 160))
        qp.drawRect(130, 15, 90, 60)

        qp.setBrush(QColor(25, 0, 90, 200))
        qp.drawRect(250, 15, 90, 60)

if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())


import multiprocessing
from os import walk, path
import os
from ProM4Py import ProM4Py
import pm4py
import time

TIMEOUT_DUR = 120000
def mine_pm4py(p_log, t):
    start = time.perf_counter()
    net, im, fm = pm4py.discover_petri_net_inductive(p_log, noise_threshold=0.2)
    end = time.perf_counter()
    t_pm4py = end - start
    t.value = t_pm4py

results_path = f'{os.getcwd()}/results/prom4py_inductive_miner.txt'
results_path_2 = f'{os.getcwd()}/results/pm4py_inductive_miner.txt'

path = f'{os.getcwd()}/real_life_logs'
filenames = next(walk(path), (None, None, []))[2]

# prom4py = ProM4Py('C:\Workspace\RWTH\prom\ProM4Py.bat', 4040, True)

if __name__ == '__main__':
    manager = multiprocessing.Manager()
    t = manager.Value('f', 0)

    for file in filenames:
        # prom4py.client.exit_handler()
        log = pm4py.read_xes(f'{path}/{file}')
        # start = time.perf_counter()
        # log = prom4py.import_log(log)
        # after_import = time.perf_counter()
        # net, im, fm = prom4py.mine_petri_net_with_inductive_miner_with_parameters(log)
        # after_mine = time.perf_counter()
        # net = net.extract()
        # end = time.perf_counter()
        # t_prom4py = end - start
        # print(t_prom4py)
        p = multiprocessing.Process(target=mine_pm4py, args=(log, t))
        p.start()

        # Wait for 10 seconds or until process finishes
        t.value = TIMEOUT_DUR
        p.join(TIMEOUT_DUR)

        # If thread is still active
        if p.is_alive():
            print("Timeout")
            # Terminate - may not work if process is stuck for good
            p.terminate()
            # OR Kill - will work for sure, no chance for process to finish nicely however
            # p.kill()
            p.join()
        print(t.value)
        # with open(results_path, "a") as myfile:
        #     myfile.write(f'{file}+{t_prom4py}+{after_import - start}+{after_mine - after_import}+{end - after_mine}\n')

        with open(results_path_2, "a") as myfile:
            myfile.write(f'{file}+{t.value}\n')


from PIL import Image, ImageDraw

path_to_row_image = "uncolored_cirqle.png"
img = Image.open(path_to_row_image)

size = img.size
height, width = size
WHITE = (255, 255, 255, 255)
BLACK = (0, 0, 0, 250)
LIGHT_BLUE = (135, 206, 250, 250)

drawMatrix = [[3] * width for i in range(0, height)]
for i in range(0, height):
    for j in range(0, width):
        if img.getpixel((i, j)) != WHITE:
            drawMatrix[i][j] = 1

filled = 0
poped = 0
stack = []
print(drawMatrix)
print(img.getcolors())

stack.append((350, 350))

while len(stack) != 0:
    pixel = stack.pop(-1)
    y, x = pixel
    print(f'Вытащил и закрасил пиксель: {(x, y)}')
    poped += 1
    filled += 1
    drawMatrix[y][x] = 2
    x_temp = x
    x += 1
    print('Пошел направо')
    while drawMatrix[y][x] != 1:
        drawMatrix[y][x] = 2
        print(f'Закрасил пиксель {(x, y)}')
        filled += 1
        x += 1
    x_right = x - 1
    x = x_temp
    x -= 1
    print('Пошел налево')
    while drawMatrix[y][x] != 1:
        drawMatrix[y][x] = 2
        print(f'Закрасил пиксель {(x, y)}')
        filled += 1
        x -= 1
    x_left = x + 1
    x = x_left
    y -= 1
    print('Пошел вверх')
    while(x <= x_right):
        flag = False
        while drawMatrix[y][x] != 2 and drawMatrix[y][x] != 1 and x < x_right:
            if not flag:
                flag = True
            print('Пошел направо в поисках незакрашенных пикселей')
            x += 1
        if flag:
            if x == x_right and drawMatrix[y][x] != 1 and drawMatrix != 2:
                print(f'Добавил пиксель {(x, y)} в стек')
                stack.append((y, x))
            else:
                print(f'Добавил пиксель {(x - 1, y)} в стек')
                stack.append((y, x - 1))
            flag = False
        x_enter = x
        while (drawMatrix[y][x] == 2 or drawMatrix[y][x] == 1) and x < x_right:
            print('Пошел направо в последнем while')
            x += 1
        if x == x_enter:
            print('Пошел направо в последнем if')
            x += 1
    x = x_left
    y += 2
    print('Пошел вниз')
    while(x <= x_right):
        flag = False
        while drawMatrix[y][x] != 2 and drawMatrix[y][x] != 1 and x < x_right:
            if not flag:
                flag = True
            print('Пошел направо в поисках незакрашенных пикселей')
            x += 1
        if flag:
            if x == x_right and drawMatrix[y][x] != 1 and drawMatrix != 2:
                print(f'Добавил пиксель {(x, y)} в стек')
                stack.append((y, x))
            else:
                print(f'Добавил пиксель {(x - 1, y)} в стек')
                stack.append((y, x - 1))
            flag = False
        x_enter = x
        while (drawMatrix[y][x] == 2 or drawMatrix[y][x] == 1) and x < x_right:
            print('Пошел направо в последнем while')
            x += 1
        if x == x_enter:
            print('Пошел направо в последнем if')
            x += 1

idDraw = ImageDraw.Draw(img)
for i in range(0, height):
    if i == width - 2:
            print("tut")
    for j in range(0, width):
        if(drawMatrix[i][j] == 2):
            idDraw.point((i, j), fill=LIGHT_BLUE)

print('Готово!')
print(f'Закрашено пикселей: {filled}')
print(f'Всего было в стеке пикселей: {poped}')
print(f'Зря побывало в стеке пикселей: {(((poped - filled) * 100) / poped):.2f} %')

img.save("line_colored_cirqle.png")


from flask import Flask, render_template, url_for, redirect, request, session, jsonify
import datetime
from dateutil.parser import parse
import requests
import io
import os
from bs4 import BeautifulSoup

from plot_types import type_to_prefix, prefix_info

app = Flask(__name__)
app.config.from_object(__name__)

SUPPORTED_SITES = ('ena', 'asi')

def instance_file(f):
    return os.path.join(app.instance_path, f)

app.config['SECRET_KEY'] = io.open(instance_file('.flask_secret'), 'rb').read()

def list_soundings(url):
    try:
        r = requests.get(url, timeout=2)
        if r.status_code != 200:
            return []

        s = BeautifulSoup(r.content, "html.parser")
        links = s.findAll('a')

        soundings = [a['href'] for a in links if 'sounding' in a['href']]

        return soundings

    except requests.exceptions.Timeout:
        return []

@app.route('/favicon.ico')
def favicon():
    return redirect(url_for("static", filename="images/favicon.ico"))

@app.route('/apple-touch-icon-precomposed.png')
@app.route('/apple-touch-icon.png')
def apple_touch():
    return redirect(url_for("static", filename="images/apple-touch-icon-precomposed.png"))

@app.route('/_get_default_prefix')
def _get_default_prefix():
    default_params = {
        'radar': 'radar-boundary-layer',
        'precip': 'precip',
        'wind': 'wind-timeseries',
        'rose': 'wind-rose',
        'uhsas': 'uhsas-distribution',
        'aerosol': 'uhsas-concentration-linear',
        'scattering': 'scattering-1um-linear',
        'ccn': 'ccn-concentration',
        'cn': 'cn-concentration-log',
        'co': 'co-concentration-linear',
        'soundings': [],
              }
    return default_params

@app.route('/_set_session_prefix', methods=['POST',])
def _set_session_prefix():

    new_prefix = request.form.get('new_prefix', None)

    if new_prefix is not None:
        plot_type = prefix_info[new_prefix]['shortname']

        current_prefixes = session['prefixes']
        current_prefixes[plot_type] = new_prefix

        session['prefixes'] = current_prefixes

        return jsonify(plot_type=plot_type, new_prefix=new_prefix)

@app.route('/', methods=['GET', ])
def index():
    return render_template('index.html')

@app.route('/date/<date>', methods=['GET', ])
def legacy_page(date):
    return redirect(url_for('figures_page', site_id='ena', date=date))

@app.route('/<site_id>', methods=['GET', 'POST'])
def site(site_id):
    if site_id not in SUPPORTED_SITES:
        return render_template('site_not_found.html')
    elif request.method == 'POST':
        date = request.form.get('date')
        if date is not None:
            return redirect(url_for('figures_page', site_id=site_id, date=date))
    else:
        default_date = (datetime.datetime.utcnow() - datetime.timedelta(days=7)).strftime('%Y-%m-%d')
        # default_date = datetime.date(2017, 1, 16)
        return render_template('site_index.html', site=site_id, date=default_date)

@app.route('/<site_id>/interesting_cases', methods=['GET', ])
def interesting_cases(site_id):
    return site_id

@app.route('/<site_id>/submit_case')
def submit_case(site_id):
    return site_id

@app.route('/<site_id>/<date>')
def figures_page(site_id, date):

    if not isinstance(date, datetime.datetime):
        try:
            dt = parse(date)
        except:
            return render_template('datetime_parse_error.html')
    else:
        dt = date

    plot_url_domain = 'http://atmos.uw.edu/~jstemm/arm-data-browser'
    plot_url = '{domain}/{site_id}/browser-figures/{date}/'.format(domain=plot_url_domain,
                                                                   site_id=site_id,
                                                                   date=dt.strftime('%Y-%m-%d'))

    global_params = {
        'date': dt,
        'next_day': dt + datetime.timedelta(days=1),
        'prev_day': dt - datetime.timedelta(days=1),
        'date_str': dt.strftime('%Y-%m-%d'),
        'plot_url': plot_url,
    }

    soundings = list_soundings(global_params['plot_url'])

    if 'prefixes' not in session:
        session['prefixes'] = _get_default_prefix()

    # session['prefixes'] = _get_default_prefix()

    return render_template('figures_page.html',
                           types=type_to_prefix,
                           labels={k: v['title'] for k,v in prefix_info.items()},
                           soundings=soundings,
                           site_id=site_id,
                           **global_params)

@app.route('/worldview/<resource>/<site_id>/<date>')
def worldview_image(resource, site_id, date):

    locs = {'asi':
                {'extent': '-70,-50,20,10',
                 'v': '-68.11415636733409,-41.439758682728495,33.13584363266591,14.739928817271503'},
            'ena':
                {'extent': '-79.875,0,0,60.75',
                 'v': '-160.453125,-57.09375,74.390625,78.1875'}}

    if resource == 'static':

        params = {'base_url': 'http://gibs.earthdata.nasa.gov/image-download',
                  'TIME': datetime.datetime.strptime(date, '%Y-%m-%d').strftime('%Y%j'),
                  'extent': locs[site_id]['extent'],
                  'layers': 'MODIS_Aqua_CorrectedReflectance_TrueColor,Coastlines',
                  'opacities': '1,1',
                  'worldfile': 'false',
                  'format': 'image/jpeg',
                  'epsg': 4326,
                  'width': '909',
                  'height': '691'}

        full_url = '{base_url}?TIME={TIME}&extent={extent}&epsg={epsg}&layers={layers}&opacities={opacities}&' \
                   'worldfile={worldfile}&format={format}&width={width}&height={height}'.format(**params)
        return redirect(full_url)

    elif resource == 'dynamic':

        base_url = 'https://worldview.earthdata.nasa.gov/'

        layers = 'MODIS_Aqua_CorrectedReflectance_TrueColor,MODIS_Terra_CorrectedReflectance_TrueColor(hidden),' \
                 'MODIS_Aqua_CorrectedReflectance_Bands721(hidden),MODIS_Terra_CorrectedReflectance_Bands721(hidden),,'\
                 'Calipso_Orbit_Asc,Coastlines,,,,,,AMSR2_Cloud_Liquid_Water_Day(hidden),'\
                 'AMSR2_Cloud_Liquid_Water_Night(hidden),' \
                 'AMSR2_Wind_Speed_Day(hidden),AMSR2_Wind_Speed_Night(hidden),'
        v = locs[site_id]['v']

        full_url = '{base_url}?p=geographic&l={layers}&t={date}&v={v}'.format(base_url=base_url,
                                                                              layers=layers, date=date, v=v)

        return redirect(full_url)

    else:

        return None

if __name__ == "__main__":
    app.run(debug=True, port=5000)


import sys

input = sys.stdin.readline
sys.setrecursionlimit(10 ** 6)

def find(x):
    if x == parent[x]: return x
    tmp = find(parent[x])
    weight[x] += weight[parent[x]]
    parent[x] = tmp
    return parent[x]

def union(x, y, w):
    if x > y:
        x, y, w = y, x, -w
    a, b = find(x), find(y)
    if a == b: return

    weight[b] = weight[x] - weight[y] + w
    parent[b] = a

while True:
    n, m = map(int, input().split())
    if not n and not m: break

    parent = [i for i in range(n + 1)]
    weight = [0 for _ in range(n + 1)]
    for _ in range(m):
        tmp = input().strip().split()
        if tmp[0] == '!':
            a, b, w = map(int, tmp[1:])
            union(a, b, w)
        else:
            a, b = map(int, tmp[1:])
            print(weight[b] - weight[a] if find(a) == find(b) else 'UNKNOWN')


import sys
import lab5_ui
from PyQt5 import QtCore, QtWidgets, QtGui
from PyQt5.QtCore import QObject, QThread, pyqtSignal
from lab5_ui import Ui_MainWindow
import socket
from time import sleep

addr = socket.getaddrinfo("192.168.184.141", 1242)[0][-1]
ServerSideSocket = socket.socket()
ServerSideSocket.bind(addr)
ServerSideSocket.listen(5)
conn, address = ServerSideSocket.accept()  # accept new connection

# Step 1: Create a worker class
class Worker(QObject):
    progress = pyqtSignal(int)

    def run(self):

        while True:
            data = conn.recv(1024).decode()
            #sleep(0.2)
            self.progress.emit(0)

class Lab5():
    def __init__(self) -> None:

        """
        Format of the data:
        b'is_active, pwm'
        """

        self.MainWindow = QtWidgets.QMainWindow()
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self.MainWindow)
        self.button_initiator()
        self.is_active = 0
        self.pwm = 0
        self.ui.PWMslider.setMaximum(180)
        self.runLongTask()

    def button_initiator(self):
        self.ui.rb_0.clicked.connect(lambda  a: self.PWM_signal(0))
        self.ui.rb_45.clicked.connect(lambda  a: self.PWM_signal(45))
        self.ui.rb_90.clicked.connect(lambda  a: self.PWM_signal(90))
        self.ui.rb_135.clicked.connect(lambda  a: self.PWM_signal(135))
        self.ui.rb_180.clicked.connect(lambda  a: self.PWM_signal(180))
        self.ui.Releasebutton.clicked.connect(self.release)
        self.ui.PWMslider.sliderReleased.connect(self.PWM_changed)

    def PWM_signal(self, num):
        self.pwm = num
        self.is_active = 1
        self.ui.PWMslider.setValue(num)

    def PWM_changed(self):
        self.PWM_signal(self.ui.PWMslider.value())
        self.ui.SignalText.setText(f"PWM signal changed to {self.ui.PWMslider.value()}")

    def release(self):
        self.is_active = 0
        self.ui.SignalText.setText(f"Button is released")

    def runLongTask(self):
        self.thread = QThread()
        self.worker = Worker()
        self.worker.moveToThread(self.thread)
        self.thread.started.connect(self.worker.run)
        self.worker.progress.connect(self.reportProgress)
        self.thread.start()

    def reportProgress(self,num):
        conn.sendall(f"{self.is_active},{self.pwm}".encode())  # send data to the client
        self.ui.SignalText.setText(f"{self.is_active},{self.pwm}")

if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)
    lab5 = Lab5()
    lab5.MainWindow.show()
    sys.exit(app.exec_())


import numpy as np
import tensorflow as tf

def get_MNIST(FLAGS):
    mnist = tf.keras.datasets.mnist
    (x_train, y_train), (x_test, y_test) = mnist.load_data()
    x_train = x_train.astype(np.float32)
    x_test  = x_test.astype(np.float32)

    x_train, x_test = x_train / 255.0, x_test / 255.0
    n_valid = 5000
    x_valid, y_valid = x_train[:n_valid], y_train[:n_valid]
    x_train, y_train = x_train[n_valid:], y_train[n_valid:]

    # shuffle so MNIST_OMNI_notMNIST can just split them at will
    def shuffle(x, y):
        idx = np.random.permutation(x.shape[0])
        return x[idx], y[idx]

    x_train, y_train = shuffle(x_train, y_train)
    x_valid, y_valid = shuffle(x_valid, y_valid)
    x_test, y_test   = shuffle(x_test, y_test)

    train = (np.reshape(x_train, [x_train.shape[0]] + FLAGS.img_shape), np.array(y_train, dtype=np.int32))
    valid = (np.reshape(x_valid, [x_valid.shape[0]] + FLAGS.img_shape), np.array(y_valid, dtype=np.int32))
    test  = (np.reshape(x_test, [x_test.shape[0]]   + FLAGS.img_shape), np.array(y_test, dtype=np.int32))

    if FLAGS.binarize_MNIST:
        def binarize_det(images, threshold=0.1):
            """Deterministic convertion into binary image. Threshold as in Deepmind's UCL module 2018."""
            return (threshold < images).astype('float32')

        def binarize_stoc(images):
            """Following https://www.cs.toronto.edu/~rsalakhu/papers/dbn_ais.pdf, which seems to be the standard reference for binarized MNIST.
            Convert stochastically into binary pixels proportionate to the picels intensities."""
            return np.random.binomial(1, images).astype('float32')

        train = (binarize_stoc(train[0]), train[1])
        import matplotlib.pyplot as plt
        valid = (binarize_stoc(valid[0]), valid[1])
        test  = (binarize_stoc(test[0]), test[1])

    return train, valid, test


D = int(input())#D=365
c = [0] + list(map(int, input().split()))#0<=c<=100
s = [0] + [[0] + list(map(int, input().split())) for _ in range(D)]#0<=s<=20000

t = [0] + [int(input()) for _ in range(D)]

M = int(input())
dq = [list(map(int, input().split())) for _ in range(M)]

held = [[0] for _ in range(27)]#選んだ日を保持
def culc_value(t):
    last = [0] * 27
    value = 0
    for d in range(1, D+1):
        type = t[d]
        held[type].append(d)
        value += s[d][type]
        last[type] = d
        for i in range(1, 27):
            value -= c[i] * (d - last[i])
    # 最終日+1 を追加、差分修正で前後見るときの後ろの最大値
    for i in range(1, 27):
        held[i].append(D+1)
    #print(held)
    return value

v_lastday = []
value = culc_value(t)

for d, q in dq:
    # 開催日の差分を修正
    value += - s[d][t[d]] + s[d][q]

    # dの前と後のq選択日のみ考えて、その間だけ修正する
    # 変換前のt[d]のマイナス計算を直す
    ind = held[t[d]].index(d)
    r = held[t[d]][ind+1]
    l = held[t[d]][ind-1]
    x = r - l
    value -= ((x + 1) * x // 2) * c[t[d]]
    y = d - l
    z = r - d
    value += (((y + 1) * y // 2) + (z + 1) * z // 2) * c[t[d]]

    del held[t[d]][ind]
    t[d] = q
    held[q].append(d)
    held[q] = sorted(held[q])
    #print(held[q])

    # 変換後のqのマイナス計算
    ind = held[q].index(d)
    r = held[q][ind+1]
    l = held[q][ind-1]
    x = r - l
    value += ((x + 1) * x // 2) * c[q]
    y = d - l
    z = r - d
    value -= (((y + 1) * y // 2) + (z + 1) * z // 2) * c[q]

    v_lastday.append(value)

for ans in v_lastday:
    print(ans)


r"""Downloads and converts Flowers data to TFRecords of TF-Example protos.

This module downloads the Flowers data, uncompresses it, reads the files
that make up the Flowers data and creates two TFRecord datasets: one for train
and one for test. Each TFRecord dataset is comprised of a set of TF-Example
protocol buffers, each of which contain a single image and label.

The script should take about a minute to run.

"""

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from multiprocessing import cpu_count

import math
import os
import random
import sys
import time
import pickle
import shutil
import threadpool
import tensorflow as tf

sys.path.append("./")
import dataset_utils

OUTPUT_PATH = ""
_RANDOM_SEED = 0

class GlobalProcess:
    def __init__(self):
        self.all_step = 0
        self.finished_file = []

    def reset(self):
        self.all_step = 0
        self.finished_file = []

    def set_all_step(self, step):
        self.all_step = step

    def add_finish(self, file):
        self.finished_file.append(file)

class ImageReader(object):
    """Helper class that provides TensorFlow image coding utilities."""

    def __init__(self):
        # Initializes function that decodes RGB JPEG data.
        self._decode_jpeg_data = tf.placeholder(dtype=tf.string)
        self._decode_jpeg = tf.image.decode_jpeg(self._decode_jpeg_data, channels=3)

    def read_image_dims(self, sess, image_data):
        image = self.decode_jpeg(sess, image_data)
        return image.shape[0], image.shape[1]

    def decode_jpeg(self, sess, image_data):
        image = sess.run(self._decode_jpeg,
                         feed_dict={self._decode_jpeg_data: image_data})
        assert len(image.shape) == 3
        assert image.shape[2] == 3
        return image

def _get_filenames_and_classes(dataset_dir):
    """Returns a list of filenames and inferred class names.

    Args:
      dataset_dir: A directory containing a set of subdirectories representing
        class names. Each subdirectory should contain PNG or JPG encoded images.

    Returns:
      A list of image file paths, relative to `dataset_dir` and the list of
      subdirectories, representing class names.
    """
    file_root = os.path.join(dataset_dir, './')
    directories = []
    class_names = []
    for filename in os.listdir(file_root):
        path = os.path.join(file_root, filename)
        if os.path.isdir(path):
            directories.append(path)
            class_names.append(filename)

    photo_filenames = []
    for directory in directories:
        for filename in os.listdir(directory):
            path = os.path.join(directory, filename)
            photo_filenames.append(path)

    return photo_filenames, sorted(class_names)

def _get_dataset_filename(dir, split_name, shard_id, num_shards):
    output_filename = '%s_%05d_of_%05d_%d.tfrecord' % (
        split_name, shard_id, num_shards, VERSION)
    return os.path.join(dir, output_filename)

def create_tf(split_name, filenames, class_names_to_ids, dataset_dir,
              num_shards, shard_id, num_per_shard, image_reader, sess):
    output_filename = _get_dataset_filename(
        OUTPUT_PATH, split_name, shard_id, num_shards)

    with tf.python_io.TFRecordWriter(output_filename) as tfrecord_writer:
        start_ndx = shard_id * num_per_shard
        end_ndx = min((shard_id + 1) * num_per_shard, len(filenames))
        for i in range(start_ndx, end_ndx):
            # sys.stdout.write('\r>> Converting image %d/%d shard %d' % (
            #     i + 1, len(filenames), shard_id))
            # sys.stdout.flush()

            # Read the filename:
            image_data = tf.gfile.FastGFile(filenames[i], 'rb').read()
            height, width = image_reader.read_image_dims(sess, image_data)

            class_name = os.path.basename(os.path.dirname(filenames[i]))
            class_id = class_names_to_ids[class_name]

            example = dataset_utils.image_to_tfexample(
                image_data, b'jpg', height, width, class_id)
            tfrecord_writer.write(example.SerializeToString())
    g_process.add_finish(output_filename)
    sys.stdout.write('\r>> Converting image %d/%d of files: %d' % (
        len(g_process.finished_file), g_process.all_step, len(filenames)))
    sys.stdout.flush()

def thread_pool(enter_func, param_list):
    pool = threadpool.ThreadPool(cpu_count())
    requests = threadpool.makeRequests(enter_func, param_list)
    [pool.putRequest(req) for req in requests]
    pool.wait()

def _convert_dataset(split_name, filenames, class_names_to_ids, dataset_dir, num_shards):
    """Converts the given filenames to a TFRecord dataset.

    Args:
      split_name: The name of the dataset, either 'train' or 'validation'.
      filenames: A list of absolute paths to png or jpg images.
      class_names_to_ids: A dictionary from class names (strings) to ids
        (integers).
      dataset_dir: The directory where the converted datasets are stored.
    """
    assert split_name in ['train', 'validation']

    num_per_shard = int(math.ceil(len(filenames) / float(num_shards)))

    with tf.Graph().as_default():
        image_reader = ImageReader()

        with tf.Session('') as sess:
            param_list = []
            for shard_id in range(num_shards):
                # create_tf(split_name, filenames, class_names_to_ids, dataset_dir,
                #           num_shards, shard_id, num_per_shard, image_reader, sess)
                param_list.append(
                    ((split_name, filenames, class_names_to_ids, dataset_dir,
                      num_shards, shard_id, num_per_shard, image_reader, sess), None))
            thread_pool(create_tf, param_list)

    sys.stdout.write('\n')
    sys.stdout.flush()

def run(dataset_dir):
    """Runs the download and conversion operation.

    Args:
      dataset_dir: The dataset directory where the dataset is stored.
    """
    if not tf.gfile.Exists(dataset_dir):
        tf.gfile.MakeDirs(dataset_dir)

    photo_filenames, class_names = _get_filenames_and_classes(dataset_dir)
    if len(photo_filenames) == 0:
        print(" no files detected")
        exit(-1)
    zz = zip(class_names, range(len(class_names)))
    class_names_to_ids = dict(zip(class_names, range(len(class_names))))

    random.seed(_RANDOM_SEED)
    random.shuffle(photo_filenames)

    labels_to_class_names = dict(zip(range(len(class_names)), class_names))
    if os.path.exists(OUTPUT_PATH) is True:
        shutil.rmtree(OUTPUT_PATH)
    if os.path.exists(OUTPUT_PATH) is False:
        os.makedirs(OUTPUT_PATH)
    dataset_utils.write_label_file(labels_to_class_names, OUTPUT_PATH)

    print("all files:%d classes: %d " % (len(photo_filenames), len(class_names)))

    _NUM_VALIDATION = math.ceil(len(photo_filenames)/10)
    training_filenames = photo_filenames[_NUM_VALIDATION:]
    validation_filenames = photo_filenames[:_NUM_VALIDATION]

    num_shards = math.ceil(len(training_filenames) / 1000)
    g_process.set_all_step(num_shards)
    _convert_dataset("train", training_filenames, class_names_to_ids,
                     dataset_dir, num_shards)

    num_shards = math.ceil(len(validation_filenames) / 1000)
    g_process.reset()
    g_process.set_all_step(num_shards)
    _convert_dataset("validation", validation_filenames, class_names_to_ids,
                     dataset_dir, num_shards)

if __name__ == "__main__":
    if len(sys.argv) < 5:
        print("usage: ./tf_train_validation_set.py -i data_set_dir -o out_put_dir")
        exit(-1)
    OUTPUT_PATH = str(sys.argv[4]+"/output_tfrecord")
    start_time = time.time()
    VERSION = int(start_time)
    g_process = GlobalProcess()
    run(sys.argv[2])
    print(' ALL COST: %.5f second' % (time.time() - start_time))