__author__ = "Connor Nelson"
from collections import Counter
import re
def array_diff(a, b):
    while b in a:
        a.remove(b)
    return a
import math
import math
def sum_of_squares(n):
    # put your code here...
    j = n
    total = 0
    count = 0
    arraySol = []
    while total < n and j > 0:
        if math.sqrt(j).is_integer() and total + j <= n:
            arraySol.append(j)
            total = total + j
            count = count + 1
            if n - total > j:
                j = j+ 1
        j = j - 1
    if total < n:
        count = count + n - total
    print (arraySol)
    return count
def top_3_words(text):
    return_list = []
    countFirst = 0
    first = ""
    countSecond = 0
    second = ""
    countThird = 0
    third = ""
    matcher = re.compile("[a-zA-Z']+")
    letterMatcher = re.compile("[a-zA-Z]+")
    words = matcher.findall(text)
    for i in range(len(words)):
        words[i] = words[i].lower()
        if "'" in words[i] and letterMatcher.search(words[i]) == None:
            words.pop(i)
    if len(set(words)) == 2:
        list_tuples = Counter(words).most_common(2)
        for i in range(len(list_tuples)):
            return_list.append(list_tuples[i][0].lower())
        return return_list
    else:
        if len(set(words)) == 1:
            list_tuples = Counter(words).most_common(1)
            for i in range(len(list_tuples)):
                return_list.append(list_tuples[i][0].lower())
            return list(set(words))
    paras = Counter(words)
    list_tuples = paras.most_common(3)
    for i in range(len(list_tuples)):
        return_list.append(list_tuples[i][0].lower())
    return return_list

    # words.remove(first)
    # second = Counter(words).most_common()
    # words.remove(second)
    # third = Counter(words).most_common()
    # return [first.lower(), second.lower(), third.lower()]

    # countsFirst = words.count(words[0])
    # countsSecond = words.count(words[0])
    # countsThird = words.count(words[0])
    # for i in set(words):
    #     if words.count(i) > countFirst:
    #       thrid = second
    #       second = first
    #         first = i
    #         countFirst = words.count(i)
    #     else:
    #       if words.count(i) > countSecond:
    #         second = i
    #         countSecond = words.count(i)
    #     else:
    #       if words.count(i) > countThird:
    #         third = i
    #         countThird = words.count(i)
    # for i in range(len(set(words)))
    #   returnArray.append()

if __name__ == "__main__":
    print(top_3_words("In a village of La Mancha, the name of which I have no desire to call to mind, there lived not long since one of those gentlemen that keep a lance in the lance-rack, an old buckler, a lean hack, and a greyhound for coursing. An olla of rather more beef than mutton, a salad on most nights, scraps on Saturdays, lentils on Fridays, and a pigeon or so extra on Sundays, made away with three-quarters of his income"))

Project 1 - Data Mining with Python/Introduction WIth Python/Charlie/CodeWarsTests.py


import datetime
import sqlite3

from matplotlib import dates
from matplotlib import ticker
from matplotlib.backends.backend_gtk3cairo import FigureCanvasGTK3Cairo as FigureCanvas
from matplotlib.dates import MonthLocator, DateFormatter
from matplotlib.figure import Figure
import gi

from .preferencesTabacchi import prefs
from . import utility

gi.require_version('Gtk', '3.0')
from gi.repository import Gtk  # noqa: E402

# Classe minimale per la visualizzazione delle statistiche
class SimpleStatsDialog(utility.GladeWindow):
    def __init__(self, parent):
        super().__init__(parent, "statsDialog.glade")

        self.statsDialog = self.builder.get_object("statsDialog")
        self.statsNotebook = self.builder.get_object("statsNotebook")
        self.consumiScrolledWindow = self.builder.get_object("consumiScrolledWindow")
        self.pesiScrolledWindow = self.builder.get_object("pesiScrolledWindow")
        self.quantitaScrolledWindow = self.builder.get_object("quantitaScrolledWindow")
        self.piuAcquistatiTreeview = self.builder.get_object("piuAcquistatiTreeview")

        self.statsDialog.set_transient_for(parent)

        self.builder.connect_signals({"on_statsDialog_delete_event": self.close,
                                      "on_statsDialog_destroy_event": self.close,
                                      "on_closeButton_clicked": self.close
                                      })
        now = datetime.datetime.now().replace(microsecond=0)
        self.dataInizioEntry = utility.DataEntry(
            self.builder.get_object("dataInizioButton"),
            now - datetime.timedelta(days=365 * 2),
            " %d %B %Y ", self.refreshData)
        self.dataFineEntry = utility.DataEntry(self.builder.get_object("dataFineButton"), now, " %d %B %Y ", self.refreshData)

        self.figureCanvas = None

    def refreshData(self):
        self.loadData()
        self.showData()

    def initData(self):
        pass

    def loadData(self):
        pass

    def showData(self):
        pass

    def showPlot(self, scrolledWindow, date, valori, unitaMin=None):
        figure = Figure(figsize=None, dpi=None)
        ax = figure.add_subplot(111)

        ax.plot_date(dates.date2num(date), valori, 'o-')

        ax.xaxis.set_major_locator(MonthLocator())
        ax.xaxis.set_major_formatter(DateFormatter('%b %y'))
        if unitaMin:
            ax.yaxis.set_major_locator(ticker.MultipleLocator(1))
            ax.yaxis.set_minor_locator(ticker.MultipleLocator(unitaMin))
        figure.autofmt_xdate()
        ax.autoscale_view()
        ax.grid(True)

        figureCanvas = FigureCanvas(figure)

        oldCanvas = scrolledWindow.get_child()
        if oldCanvas:
            scrolledWindow.remove(oldCanvas)

        scrolledWindow.add_with_viewport(figureCanvas)
        figureCanvas.show()

    def run(self):
        self.result = self.statsDialog.run()
        return self.result

    def close(self, widget, other=None):
        self.result = Gtk.ResponseType.CANCEL
        self.statsDialog.destroy()

class StatsDialog(SimpleStatsDialog):
    def __init__(self, parent, idPar):
        SimpleStatsDialog.__init__(self, parent)
        self.statsNotebook.remove_page(3)
        self.id = idPar
        self.initData()
        self.loadData()

        self.statsDialog.set_title(self.descrizione)
        self.showData()

    # Inizializzazione
    def initData(self):
        self.date = []
        self.consumi = []
        self.giacenze = []
        self.ordini = []
        self.unitaMin = None
        self.descrizione = None

    # Legge i dati
    def loadData(self):
        cursor = None
        conn = None
        try:
            conn = prefs.getConn()
            cursor = prefs.getCursor(conn)
            cursor.execute("SELECT t.Descrizione, t.UnitaMin FROM tabacchi t WHERE t.ID = ?", (self.id,))
            row = cursor.fetchone()
            self.descrizione = row['Descrizione']
            self.unitaMin = row['UnitaMin']
            dataFine = self.dataFineEntry.data.strftime("%Y-%m-%d")
            dataInizio = self.dataInizioEntry.data.strftime("%Y-%m-%d")
            cursor.execute(
                "SELECT r.Consumo, r.Giacenza, r.Ordine, o.Data as 'Data [timestamp]' FROM rigaOrdineTabacchi r, ordineTabacchi o where (r.ID_Ordine = o.ID) and (r.ID = ?) and (o.Data < ?) and (o.Data > ?) order by o.Data",
                (self.id, dataFine, dataInizio))
            result_set = cursor.fetchall()
            self.date[:] = []
            self.consumi[:] = []
            self.giacenze[:] = []
            self.ordini[:] = []
            oldOrdine = 0
            for row in result_set:
                data = row['Data']
                consumo = round(row['Consumo'], 3)
                ordine = round(row['Ordine'], 3)
                giacenza = round(row['Giacenza'], 3) - oldOrdine
                self.date.append(data)
                self.consumi.append(consumo if (consumo > 0) else 0)
                self.ordini.append(ordine if (ordine > 0) else 0)
                self.giacenze.append(giacenza if (giacenza > 0) else 0)
                oldOrdine = ordine
        except sqlite3.Error as e:
            utility.gtkErrorMsg(e, self.statsDialog)
        finally:
            if cursor:
                cursor.close()
            if conn:
                conn.close()

    def showData(self):
        self.showPlot(self.consumiScrolledWindow, self.date, self.consumi, self.unitaMin)
        self.showPlot(self.pesiScrolledWindow, self.date, self.ordini, self.unitaMin)
        self.showPlot(self.quantitaScrolledWindow, self.date, self.giacenze, self.unitaMin)

class GlobalStatsDialog(SimpleStatsDialog):
    modelInfoList = [("+Descrizione", "str"), ("^Totale acquisti", "float"), ("^Data ultimo acquisto", "date")]

    def __init__(self, parent):
        SimpleStatsDialog.__init__(self, parent)

        utility.ExtTreeView(self.modelInfoList, self.piuAcquistatiTreeview, formats={1: "%.3f kg"})
        self.piuAcquistatiModel = self.piuAcquistatiTreeview.get_model()
        self.piuAcquistatiTreeview.set_model(None)
        self.initData()
        self.loadData()
        self.piuAcquistatiTreeview.set_model(self.piuAcquistatiModel)

        self.statsDialog.set_title("Statistiche generali")

        self.showData()

    # Inizializzazione
    def initData(self):
        self.date = []
        self.consumi = []
        self.giacenze = []
        self.ordini = []

    # Legge i dati

    def loadData(self):
        cursor = None
        conn = None
        try:
            conn = prefs.getConn()
            cursor = prefs.getCursor(conn)
            dataFine = self.dataFineEntry.data.strftime("%Y-%m-%d")
            dataInizio = self.dataInizioEntry.data.strftime("%Y-%m-%d")
            cursor.execute(
                "SELECT t.Descrizione, SUM(r.Ordine) totPeso, MAX(o.Data) as 'maxData [timestamp]' FROM rigaOrdineTabacchi r, ordineTabacchi o, tabacchi t where (r.ID = t.ID) and t.InMagazzino and (r.ID_Ordine = o.ID) and (r.Ordine > 0) and (o.Data < ?) and (o.Data > ?) group by r.ID order by totPeso desc",
                (dataFine, dataInizio))
            result_set = cursor.fetchall()
            self.piuAcquistatiModel.clear()
            for row in result_set:
                self.piuAcquistatiModel.append([row["Descrizione"], row["totPeso"], row["maxData"]])
            cursor.execute(
                "SELECT SUM(r.Consumo) as Consumo, SUM(r.Giacenza) as Giacenza, SUM(r.Ordine) as Ordine, o.Data as 'Data [timestamp]' FROM rigaOrdineTabacchi r, ordineTabacchi o where (r.ID_Ordine = o.ID) and ((r.Consumo > 0) or (r.Giacenza > 0) or (r.Ordine > 0)) and (o.Data < ?) and (o.Data > ?) group by o.ID order by o.Data",
                (dataFine, dataInizio))
            result_set = cursor.fetchall()

            self.date[:] = []
            self.consumi[:] = []
            self.giacenze[:] = []
            self.ordini[:] = []
            # oldOrdine = 0
            for row in result_set:
                data = row['Data']
                consumo = round(row['Consumo'], 3)
                ordine = round(row['Ordine'], 3)
                esistenza = round(row['Giacenza'], 3)
                giacenza = esistenza  # - oldOrdine

                self.date.append(data)
                self.consumi.append(consumo if (consumo > 0) else 0)
                self.ordini.append(ordine if (ordine > 0) else 0)
                self.giacenze.append(giacenza if (giacenza > 0) else 0)
                # oldOrdine = ordine
        except sqlite3.Error as e:
            utility.gtkErrorMsg(e, self.statsDialog)
        finally:
            if cursor:
                cursor.close()
            if conn:
                conn.close()

    def showData(self):
        self.showPlot(self.consumiScrolledWindow, self.date, self.consumi)
        self.showPlot(self.pesiScrolledWindow, self.date, self.ordini)
        self.showPlot(self.quantitaScrolledWindow, self.date, self.giacenze)


import inspect

import numpy as np

from hypothesis import given, example, settings

from pytest import raises, warns

from ..ndindex import ndindex, asshape
from ..integer import Integer
from ..ellipsis import ellipsis
from ..integerarray import IntegerArray
from ..tuple import Tuple
from .helpers import ndindices, check_same, assert_equal

@given(ndindices)
def test_eq(idx):
    index = ndindex(idx)
    new = type(index)(*index.args)
    assert (new == index) is True
    try:
        if isinstance(new.raw, np.ndarray):
            raise ValueError
        assert (new.raw == index.raw) is True
        assert (index.raw == index) is True
    except ValueError:
        np.testing.assert_equal(new.raw, index.raw)
        # Sadly, there is now way to bypass array.__eq__ from producing an
        # array.
    assert hash(new) == hash(index)
    assert (index == index.raw) is True
    assert (index == 'a') is False
    assert ('a' == index) is False
    assert (index != 'a') is True
    assert ('a' != index) is True

def test_eq_explicit():
    assert Integer(0) != False
    assert Integer(1) != True
    assert Integer(0) != IntegerArray(0)

@example((np.array([1, 2]), 0))
@example([1, 2, 3])
@given(ndindices)
def test_ndindex(idx):
    index = ndindex(idx)
    assert index == idx
    def test_raw_eq(idx, index):
        if isinstance(idx, np.ndarray):
            assert_equal(index.raw, idx)
        elif isinstance(idx, list):
            assert index.dtype in [np.intp, np.bool_]
            assert_equal(index.raw, np.asarray(idx, dtype=index.dtype))
        elif isinstance(idx, tuple):
            assert type(index.raw) == type(idx)
            assert len(index.raw) == len(idx)
            assert index.args == index.raw
            for i, j in zip(idx, index.args):
                test_raw_eq(i, j)
        else:
            assert index.raw == idx
    test_raw_eq(idx, index)
    assert ndindex(index.raw) == index

def test_ndindex_invalid():
    a = np.arange(10)
    for idx in [1.0, [1.0], np.array([1.0]), np.array([1], dtype=object),
                np.array([])]:
        check_same(a, idx)

    # This index is allowed by NumPy, but gives a deprecation warnings. We are
    # not going to allow indices that give deprecation warnings in ndindex.
    with warns(None) as r: # Make sure no warnings are emitted from ndindex()
        raises(IndexError, lambda: ndindex([1, []]))
    assert not r

def test_ndindex_ellipsis():
    raises(IndexError, lambda: ndindex(ellipsis))

def test_signature():
    sig = inspect.signature(Integer)
    assert sig.parameters.keys() == {'idx'}

@example(([0, 1],))
@example((IntegerArray([], (0, 1)),))
@example(IntegerArray([], (0, 1)))
@example((1, ..., slice(1, 2)))
# eval can sometimes be slower than the default deadline of 200ms for large
# array indices
@settings(deadline=None)
@given(ndindices)
def test_repr_str(idx):
    # The repr form should be re-creatable
    index = ndindex(idx)
    d = {}
    exec("from ndindex import *", d)
    assert eval(repr(index), d) == idx

    # Str may not be re-creatable. Just test that it doesn't give an exception.
    str(index)

def test_asshape():
    assert asshape(1) == (1,)
    assert asshape(np.int64(2)) == (2,)
    assert type(asshape(np.int64(2))[0]) == int
    assert asshape((1, 2)) == (1, 2)
    assert asshape([1, 2]) == (1, 2)
    assert asshape((np.int64(1), np.int64(2))) == (1, 2)
    assert type(asshape((np.int64(1), np.int64(2)))[0]) == int
    assert type(asshape((np.int64(1), np.int64(2)))[1]) == int

    raises(TypeError, lambda: asshape(1.0))
    raises(TypeError, lambda: asshape((1.0,)))
    raises(ValueError, lambda: asshape(-1))
    raises(ValueError, lambda: asshape((1, -1)))
    raises(TypeError, lambda: asshape(...))
    raises(TypeError, lambda: asshape(Integer(1)))
    raises(TypeError, lambda: asshape(Tuple(1, 2)))
    raises(TypeError, lambda: asshape((True,)))


import importlib
import os
import re
import sys

from cpt.packager import ConanMultiPackager

spec = importlib.util.spec_from_file_location('comm', 'scripts/common.py')
mod = importlib.util.module_from_spec(spec)
spec.loader.exec_module(mod)

if __name__ == "__main__":
    projname = os.getenv('CONAN_PACKAGE_NAME')
    if not projname:
        raise Exception('CONAN_PACKAGE_NAME environment variable not defined')
    username = os.getenv('CONAN_USERNAME')
    if not username:
        raise Exception('CONAN_USERNAME environment variable not defined')
    branch = os.getenv('TRAVIS_BRANCH')
    if not branch:
        raise Exception('TRAVIS_BRANCH environment variable not defined (are you not releasing on Travis?)')

    matched, projver = mod.is_dev_branch()
    channel = 'nightly'
    if not matched:
        matched, projver = mod.is_rel_branch()
        channel = 'stable'

    if matched:
        reference = f'{projname}/{projver}@{username}/{channel}'

        builder = ConanMultiPackager(reference=reference)

        builder.add({}, {}, {}, {})
        builder.run()
    else:
        print('Not release or development branch, ignoring release!')


from typing import List, Tuple, Dict

def main():
    with open("advent_2019_day8.txt") as my_input:
        data = my_input.read()
        layers = []

        image = []
        while len(image) < 2:
            row = []
            while len(row) < 3:
                for j in data:
                    row.append(j)
            image.append(row)
        layers.append(image)
        print(layers)

if __name__ == "__main__":
    main()


from os import walk
from os.path import join
from os.path import isdir
import imghdr

def getPicturesByFolder(mainPath, folder):

  imageExtensionAccepted = ['jpeg', 'png']
  filesInFolder = []
  imagesInFolder = []

  fullFolder = join(mainPath,folder)
  for (dirpath, dirnames, filenames) in walk(fullFolder):
    #filesInFolder.extend([join(dirpath,f) for f in filenames])
    for filename in filenames:
      filesInFolder.append({'dir':dirpath, 'filename': filename})
    break # we stop the list at the first level of the directory (we don't look in subfolders)

  for myfile in filesInFolder:
    if imghdr.what(join(myfile['dir'],myfile['filename'])) in imageExtensionAccepted:
      imagesInFolder.append(myfile)

  return imagesInFolder

def checkFolderExistence(mainPath,innerFolder):
  fullFolder = join(mainPath,innerFolder)
  return isdir(fullFolder)


"""Fake FLI USB camera module"""

import numpy as np
import astropy.io.fits as pyfits
import time
import os
import threading

class FliError(Exception):
    """Exception for FLI camera"""
    pass

def numberOfCamera():
    """Return number of available FLI cameras"""
    return numCams

def getLibVersion():
    """Get the current library version"""
    return "Software Development Library for Linux 1.999.1"

class Camera:
    """FLI usb camera"""

    def __init__(self, id, devsn, imgPath=None):
        """(id) : index of the camera device"""
        if id < 0 or id >= numCams:
            raise FliError("Camera[%d] not available" % id)
        self.id = id
        self.status = CLOSED
        self.exposureID = 0
        self.agcid = -1
        self.abort = 0
        self.temp = None
        self.devname = "MicroLine ML4720"
        self.devsn = devsn
        self.hwRevision = 256
        self.fwRevision = 512
        self.mode = 0

        # read simulated image, contains single or 6 image extensions
        if imgPath is not None:
            hdulist = pyfits.open(imgPath)
            if len(hdulist) > 1:
                if hdulist[id+1].data is None:
                    self.rawdata = np.zeros((1033, 1072), dtype=np.uint16)
                else:
                    self.rawdata = hdulist[id+1].data.astype(np.uint16)
            else:
                self.rawdata = hdulist[0].data.astype(np.uint16)
        else:
            self.rawdata = np.zeros((1033, 1072), dtype=np.uint16)
        self.lock = threading.Lock()

    def getStatusStr(self):
        with self.lock:
            status = self.status
        return Status[status]

    def isClosed(self):
        with self.lock:
            status = self.status
        return status == CLOSED

    def isReady(self):
        with self.lock:
            status = self.status
        return status == READY

    def isExposing(self):
        with self.lock:
            status = self.status
        return status == EXPOSING

    def isSetmode(self):
        with self.lock:
            status = self.status
        return status == SETMODE

    def open(self):
        """Open the camera device"""
        if dev[self.id] != FLI_INVALID_DEVICE:
            raise FliError("Device already opened")
        dev[self.id] = FLIDEVICE_CAMERA

        # set default parameters
        self.setTemperature(CCD_TEMP)
        self.setHBin(1)
        self.setVBin(1)
        self.setExpTime(0)
        self.setFrame(0, 0, 1072, 1033)
        with self.lock:
            self.status = READY
            self.visibleExpArea = (24, 9, 1048, 1033)
            self.defaultExpArea = (0, 0, 1072, 1033)
            self.expArea = (0, 0, 1072, 1033)
            self.regions = ((0, 0, 0), (0, 0, 0))

    def close(self):
        """Close the camera device"""
        if dev[self.id] == FLI_INVALID_DEVICE:
            raise FliError("Device already closed or not initialized")
        dev[self.id] = FLI_INVALID_DEVICE
        with self.lock:
            self.status = CLOSED

    def setExpTime(self, exptime):
        """Set the exposure time in ms"""
        with self.lock:
            self.exptime = exptime

    def setHBin(self, hbin):
        """Set the horizontal binning"""
        with self.lock:
            self.hbin = hbin

    def setVBin(self, vbin):
        """Set the vertical binning"""
        with self.lock:
            self.vbin = vbin

    def setFrame(self, x1, y1, width, height):
        """Set the image area"""
        with self.lock:
            self.xsize = width
            self.ysize = height
            self.expArea = (x1, y1, x1+width, y1+height)

    def resetFrame(self):
        """Reset the image area"""
        with self.lock:
            hbin = self.hbin
            vbin = self.vbin
        if hbin != 1:
            self.setHBin(1)
        if vbin != 1:
            self.setVBin(1)
        with self.lock:
            self.expArea = self.defaultExpArea
            x1, y1, x2, y2 = self.expArea
            self.xsize = x2 - x1
            self.ysize = y2 - y1

    def setTemperature(self, temp):
        """Set the CCD temperature"""
        with self.lock:
            self.temp = temp

    def getTemperature(self):
        """Get the CCD temperature"""
        with self.lock:
            temp = self.temp
        return temp

    def getCoolerPower(self):
        """Get the cooler power in percentage"""
        return 90.0

    def getPixelSize(self):
        """Get the pixel sizes in micron"""
        return (0.000013, 0.000013)

    def wfits(self, filename=None):
        """Write the image to a FITS file"""
        with self.lock:
            dark = self.dark
        if not filename:
            if dark != 0:
                filename = self.getNextFilename("dark")
            else:
                filename = self.getNextFilename("object")
        with self.lock:
            if(self.data.size == 0):
                raise FliError("No image available")
            hdu = pyfits.PrimaryHDU(self.data)
        hdr = hdu.header
        with self.lock:
            hdr.set('DATE', self.timestamp, 'exposure begin date')
            hdr.set('INSTRUME', self.devname, 'this instrument')
            hdr.set('SERIAL', self.devsn, 'serial number')
            hdr.set('EXPTIME', self.exptime, 'exposure time (ms)')
            hdr.set('VBIN', self.vbin, 'vertical binning')
            hdr.set('HBIN', self.hbin, 'horizontal binning')
            hdr.set('CCD-TEMP', self.temp, 'CCD temperature')
            if dark != 0:
                hdr.set('SHUTTER', 'CLOSE', 'shutter status')
            else:
                hdr.set('SHUTTER', 'OPEN', 'shutter status')
            hdr.set('CCDAREA', '[%d:%d,%d:%d]' % self.expArea, 'image area')
        hdu.writeto(filename, overwrite=True, checksum=True)
        with self.lock:
            self.filename = filename

    def getNextFilename(self, expType):
        """Fetch the next image filename"""
        with self.lock:
            self.exposureID += 1
            exposureID = self.exposureID
        path = os.path.join("$ICS_MHS_DATA_ROOT", 'agcc')
        path = os.path.expandvars(os.path.expanduser(path))
        if not os.path.isdir(path):
            os.makedirs(path, 0o755)
        with self.lock:
            timestamp = self.timestamp
        return os.path.join(path, 'AGC%d_%s_%06d_%s.fits' % \
               (self.agcid + 1, expType, exposureID, timestamp))

    def cancelExposure(self):
        """Cancel current exposure"""
        with self.lock:
            status = self.status
        if status == EXPOSING:
            with self.lock:
                self.abort = 1

    def expose(self, dark=False, blocking=True):
        """Do exposure and return the image"""
        with self.lock:
            status = self.status
        if status != READY:
            raise FliError("Camera not ready, abort expose command")
        with self.lock:
            self.dark = dark
            self.tstart = time.time()
            self.timestamp = time.strftime("%Y-%m-%dT%H:%M:%S", time.localtime(self.tstart))
            self.status = EXPOSING

        thr = threading.Thread(target=self.exposeHandler)
        thr.start()
        if blocking:
            thr.join()

    def exposeHandler(self):
        # Check if the exposure is done and write the image
        tstart = time.time();
        with self.lock:
            # add 350ms readout time
            exptime = (self.exptime + 350.0) / 1000.0
        while (time.time() - tstart < exptime):
            time.sleep(POLL_TIME)
            with self.lock:
                abort = self.abort
            if abort != 0:
                break

        with self.lock:
            if self.abort != 0:
                # Exposure aborted
                self.abort = 0
                self.tend = 0
            else:
                xsize = self.xsize
                ysize = self.ysize
                self.data = self.rawdata[self.expArea[1]:self.expArea[3], self.expArea[0]:self.expArea[2]]
                self.tend = time.time()
            self.status = READY

    def expose_test(self):
        """Return the test image"""
        with self.lock:
            self.dark = 1
            self.tstart = time.time()
            self.timestamp = time.strftime("%Y-%m-%dT%H:%M:%S", time.localtime(self.tstart))
            imagesize = (self.expArea[3] - self.expArea[1],
                         self.expArea[2] - self.expArea[0])
            self.data = np.ones(shape=imagesize, dtype=np.uint16)
            self.tend = time.time()

    def getModeString(self, mode):
        """Get the camera mode string"""
        if mode == 0:
            return "4 MHz"
        elif mode == 1:
            return "500 KHz"
        else:
            raise FliError("FLIGetCameraModeString failed")

    def getMode(self):
        """Get the camera mode string"""
        with self.lock:
            mode = self.mode
        return mode

    def setMode(self, mode):
        """Get the camera mode string"""
        if mode == 0 or mode == 1:
            with self.lock:
                self.mode = mode
        else:
            raise FliError("FLISetCameraMode failed")

    def getTotalTime(self):
        """ get the total readout + exposure time in second """
        with self.lock:
            if self.tend == 0:
                total = -1
            else:
                total = self.tend - self.tstart
        return total

# module initialization
CLOSED, READY, EXPOSING, SETMODE = range(4)
Status = {CLOSED:"CLOSED", READY:"READY", EXPOSING:"EXPOSING", SETMODE:"SETMODE"}
POLL_TIME = 0.02
CCD_TEMP = -30
FLI_INVALID_DEVICE, FLIDEVICE_CAMERA = 0, 1

numCams = 6
dev = np.zeros(numCams, int)


import keras
from keras.models import *
from keras.layers import *
import tensorflow as tf
from .config import IMAGE_ORDERING

if IMAGE_ORDERING == 'channels_first':
    MERGE_AXIS = 1
elif IMAGE_ORDERING == 'channels_last':
    MERGE_AXIS = -1

def BR(input):
    output = BatchNormalization()(input)
    output = PReLU()(output)
    return output

def CDilated(input, filters, kernel, stride, dilate):
    output = Conv2D(filters= filters, kernel_size = kernel, strides = stride, dilation_rate = dilate, padding = 'same', data_format = IMAGE_ORDERING)(input)
    return output

def get_custom_encoder_v1(input_height=512,  input_width=512):

    if IMAGE_ORDERING == 'channels_first':
        img_input = Input(shape=(3, input_height, input_width))
    elif IMAGE_ORDERING == 'channels_last':
        img_input = Input(shape=(input_height, input_width, 3))

    levels = []
    x = Conv2D(filters = 32, kernel_size = 3, strides = 2, padding = 'same', data_format = IMAGE_ORDERING)(img_input)
    #print(f'Conv1_1: {x.shape}')
    x = CDilated(x, 32, 3, 1, 2)
    x = BR(x)
    #print(f'Conv1_2: {x.shape}')
    pool1 = AveragePooling2D(pool_size=(3,3), strides=2, padding='same')(img_input)
    #print(f'Pool1: {pool1.shape}')
    x = Concatenate(axis=MERGE_AXIS)([x, pool1])
    #print(f'Concat1: {x.shape}')
    levels.append(x)
    x = Conv2D(filters = 64, kernel_size = 3, strides = 2, padding = 'same', data_format = IMAGE_ORDERING)(x)
    #print(f'Conv2_1: {x.shape}')
    x = CDilated(x, 64, 3, 1, 4)
    x = BR(x)
    #print(f'Conv2_2: {x.shape}')
    pool2 = AveragePooling2D(pool_size=(3,3), strides=2, padding='same')(pool1)
    #print(f'Pool2: {pool2.shape}')
    x = Concatenate(axis=MERGE_AXIS)([x, pool2])
    #print(f'Concat2: {x.shape}')
    levels.append(x)
    x = Conv2D(filters = 128, kernel_size = 3, strides = 2, padding = 'same', data_format = IMAGE_ORDERING)(x)
    #print(f'Conv3_1: {x.shape}')
    x = CDilated(x, 128, 3, 1, 8)
    x = BR(x)
    #print(f'Conv3_2: {x.shape}')
    pool3 = AveragePooling2D(pool_size=(3,3), strides=2, padding='same')(pool2)
    #print(f'Pool3: {pool2.shape}')
    x = Concatenate(axis=MERGE_AXIS)([x, pool3])
    x = BR(x)
    #print(f'Concat3: {x.shape}')
    levels.append(x)
    x = Conv2D(filters = 256, kernel_size = 3, strides = 2, padding='same',data_format = IMAGE_ORDERING)(x)
    x = BR(x)
    #print(f'Conv4: {x.shape}')
    levels.append(x)
    return img_input, levels


import pymysql
from datetime import datetime
from db import Db, WriteErrorToLog, log_file
from db_create import QueryCreateRecipe, QueryCreateMethodItems

####################### CLASS FOR SQL QUERY TO DELETE RECIPE BASED ON RECIPE ID #########################

class QueryDeleteRecipe():

    def __init__(self, recipe_id, table):
        self.recipe_id = recipe_id
        self.table = table

    def delete(self):
        try:
            with Db(commit=True) as cursor:
                cursor.execute(""" DELETE FROM %s
                                    WHERE RecipeId = %s;
                                """ % (self.table, self.recipe_id))
        except pymysql.err.OperationalError as e:
            message = " FAILED: delete method in db_read.QueryDeleteRecipes."
            log = WriteErrorToLog(str(e), message, log_file, str(datetime.now()))
            log.write_to_doc()
        else:
            print("delete completed")

    def delete_saved_recipe(self, user_id):
        try:
            with Db(commit=True) as cursor:
                cursor.execute(""" DELETE FROM %s
                                    WHERE UserId = %s
                                    AND RecipeId = %s;""" % (self.table, user_id, self.recipe_id))
        except pymysql.err.OperationalError as e:
            message = " FAILED: delete_saved_recipe in db_read.QueryDeleteRecipes."
            log = WriteErrorToLog(str(e), message, log_file, str(datetime.now()))
            log.write_to_doc()
        else:
            print("delete_saved_recipe completed")

############################### CLASS FOR SQL UPDATE QUERIES ############################################
#########################################################################################################

class UpdateQuery():

    recipe_query = """ UPDATE Recipe SET
                        RecipeTitle = %s,
                        RecipeDescription = %s,
                        CookingTimeMins = %s,
                        MakePublic = %s
                        WHERE RecipeId = %s;
                    """

    def single_column_query(self, table, column, column_value, recipe_id):
        query = """ UPDATE %s SET
                    %s = '%s'
                    WHERE RecipeId = %s;
                """ % (table, column, column_value, recipe_id)
        return query

###################### CLASS FOR EXECUTING UPDATE QUERIES TO RECIPE TABLE AND STATS TABLES ###############

class QueryUpdateRecipe(QueryCreateRecipe):

    def __init__(self, recipe, user_id, recipe_id):
        super().__init__(recipe, user_id)
        self.recipe_id = recipe_id

    def update_recipe(self):
        recipe_values = (self.recipe_title, self.recipe_description,
                            self.cooking_time, self.make_public, self.recipe_id)
        try:
            with Db(commit=True) as cursor:
                cursor.execute(UpdateQuery.recipe_query, recipe_values)
        except pymysql.err.OperationalError as e:
            message = " FAILED: update_recipe in db_read.QueryUpdateRecipes."
            log = WriteErrorToLog(str(e), message, log_file, str(datetime.now()))
            log.write_to_doc()
        else:
            print("update_recipe completed")

    def update_stats(self):
        stat_queries = [[UpdateQuery.single_column_query(self,'Cuisine','CuisineName',
                                                        self.cuisine_name, self.recipe_id)],
                    [UpdateQuery.single_column_query(self,'Course','CourseName',
                                                        self.course_name, self.recipe_id)],
                    [UpdateQuery.single_column_query(self,'Health','Calories',
                                                        self.calories, self.recipe_id)],
                    [UpdateQuery.single_column_query(self,'Cost','Price',
                                                         self.cost, self.recipe_id)],
                    [UpdateQuery.single_column_query(self,'Servings','Servings',
                                                        self.servings, self.recipe_id)]]
        try:
            with Db(commit=True) as cursor:
                for stat in stat_queries:
                    cursor.execute(stat[0])
        except pymysql.err.OperationalError as e:
            message = " FAILED: update_recipe in db_read.QueryUpdateRecipes."
            log = WriteErrorToLog(str(e), message, log_file, str(datetime.now()))
            log.write_to_doc()
        else:
            print("update_recipe completed")


import sys

'''
Sort speling file by lemma.
'''

inp = sys.stdin.readlines()

v = {}
for l in inp:
    l = l.strip('\n')
    p = l.split('; ')
    k = p[0] + "__" + p[3]
    if k not in v:
        v[k] = [l]
    else:
        v[k] += [l]

for l in v:
    for e in v[l]:
        print(e)


from player import Player
import numpy as np
from config import CONFIG
import copy
import math
from nn import min_random, max_random
from util import make_stat_files

class Evolution:

    def __init__(self, mode):
        self.mode = mode
        self.gen_num = 1
        self.mutation_count = 0
        self.crossover_count = 0

    # calculate fitness of players
    def calculate_fitness(self, players, delta_xs):
        for i, p in enumerate(players):
            p.fitness = delta_xs[i]

    def mutate(self, child):
        mutation_distribution = [0.2, 0.8]  # from each 10 values 1 value will change
        mutation_occurrence = [True, False]

        # mutating weights
        for w in child.nn.w[1:]:
            shape = w.shape
            mutations = []
            for i in range(shape[0]):
                for j in range(shape[1]):
                    if np.random.choice(mutation_occurrence, 1, p=mutation_distribution)[0]:
                        mutations.append((i, j))

            for mutation in mutations:
                # make random number in [-1, 1)
                # print(f'mutation on : {mutation}, before : {w[mutation]}', end='')
                w[mutation] = np.random.random(size=1)[0] * (max_random - min_random) + min_random
                # print(f', after: {w[mutation]}')

        # mutating biases
        for b in child.nn.b[1:]:
            shape = b.shape
            mutations = []
            for i in range(shape[0]):
                for j in range(shape[1]):
                    if np.random.choice(mutation_occurrence, 1, p=mutation_distribution)[0]:
                        mutations.append((i, j))

            for mutation in mutations:
                # make random number in [-1, 1)
                b[mutation] = np.random.random(size=1)[0] * (max_random - min_random) + min_random

    def crossover(self, parent1, parent2):
        child1_biases = [None]
        child2_biases = [None]

        child1_weights = [None]
        child2_weights = [None]

        for bias_index in range(1, len(parent1.nn.b)):
            shape = parent1.nn.b[bias_index].shape
            p1_bias = parent1.nn.b[bias_index].flatten()
            p2_bias = parent2.nn.b[bias_index].flatten()

            c1_bias = []
            c2_bias = []

            slice_index = math.floor(len(p1_bias) / 2)

            c1_bias.extend(p1_bias[:slice_index])
            c1_bias.extend(p2_bias[slice_index:])

            c2_bias.extend(p2_bias[:slice_index])
            c2_bias.extend(p1_bias[slice_index:])

            child1_biases.append(np.array(c1_bias).reshape(shape))
            child2_biases.append(np.array(c2_bias).reshape(shape))

        for weight_index in range(1, len(parent1.nn.w)):
            shape = parent1.nn.w[weight_index].shape
            p1_weight = parent1.nn.w[weight_index].flatten()
            p2_weight = parent2.nn.w[weight_index].flatten()

            c1_weight = []
            c2_weight = []

            slice_index = math.floor(len(p1_weight) / 2)

            c1_weight.extend(p1_weight[:slice_index])
            c1_weight.extend(p2_weight[slice_index:])

            c2_weight.extend(p2_weight[:slice_index])
            c2_weight.extend(p1_weight[slice_index:])

            child1_weights.append(np.array(c1_weight).reshape(shape))
            child2_weights.append(np.array(c2_weight).reshape(shape))

        return child1_biases, child1_weights, child2_biases, child2_weights

    def generate_new_population(self, num_players, prev_players=None):

        # in first generation, we create random players
        if prev_players is None:
            return [Player(self.mode) for _ in range(num_players)]

        else:
            mutations_happens = 0
            crossover_happens = 0

            # num_players example: 150
            mutation_distribution = [0.9, 0.1]
            mutation_occurrence = [True, False]

            crossover_distribution = [0.6, 0.4]
            crossover_occurrence = [True, False]

            fitness_list = np.array([player.fitness for player in prev_players])
            fitness_list = fitness_list ** 1.3
            distribution = fitness_list / sum(fitness_list)

            candidates_indices = list(np.random.choice(range(0, len(prev_players)), num_players, p=distribution, replace=True))

            new_players = []
            while candidates_indices:
                assert len(candidates_indices) % 2 == 0, 'the num_players must be even number'
                parent1_index = candidates_indices.pop(0)
                parent2_index = candidates_indices.pop(0)

                parent1 = prev_players[parent1_index]
                parent2 = prev_players[parent2_index]

                child1 = copy.deepcopy(parent1)
                child2 = copy.deepcopy(parent2)

                do_crossover = np.random.choice(crossover_occurrence, 1, p=crossover_distribution)[0]
                do_mutate_child1 = np.random.choice(mutation_occurrence, 1, p=mutation_distribution)[0]
                do_mutate_child2 = np.random.choice(mutation_occurrence, 1, p=mutation_distribution)[0]

                if do_crossover:
                    crossover_happens += 1
                    child1_biases, child1_weights, child2_biases, child2_weights = self.crossover(parent1, parent2)
                    child1.nn.set_biases(child1_biases)
                    child1.nn.set_weights(child1_weights)
                    child2.nn.set_biases(child2_biases)
                    child2.nn.set_weights(child2_weights)

                if do_mutate_child1:
                    mutations_happens += 1
                    self.mutate(child1)
                if do_mutate_child2:
                    mutations_happens += 1
                    self.mutate(child2)

                new_players.append(child1)
                new_players.append(child2)

            self.mutation_count = mutations_happens
            self.crossover_count = crossover_happens
            # print(f'mutations : {mutations_happens}, crossovers: {crossover_happens}')
            return new_players

    def next_population_selection(self, players, num_players, mode='QT'):
        # Q tournament
        selected_players = []
        selected_players_indices = []
        if mode == 'QT':
            tournament_size = 3
            for _ in range(num_players):
                tournament_candidates_indices = np.random.randint(0, high=len(players), size=tournament_size)
                tournament_candidates = [(i, players[i]) for i in tournament_candidates_indices]
                tournament_candidates.sort(key=lambda x: x[1].fitness, reverse=True)
                selected_players.append(copy.deepcopy(tournament_candidates[0][1]))
                selected_players_indices.append(tournament_candidates[0][0])

        # top-k
        else:
            candidates = [(i, players[i]) for i in range(len(players))]
            candidates.sort(key=lambda x: x[1].fitness, reverse=True)
            selected_players = [x[1] for x in candidates[:num_players]]
            selected_players_indices = [x[0] for x in candidates[:num_players]]

        make_stat_files(self.mode, self.gen_num, self.mutation_count, self.crossover_count, players, selected_players_indices)
        #
        self.gen_num += 1
        return selected_players


#!/usr/bin/env python3

import datetime
from datetime import timedelta
from xmlrpc.client import FastParser
import PluginsPy
import os
import xlwt

from tools.ShellCMD import Shell

@PluginsPy.addRun
class RepoExcel:

    """
    处理二进制文件

    @dir(../../android/merge/): input dir
    @last(7): 最后几天时间
    """

    def __init__(self, kwargs):

        repoDir = kwargs["dir"]
        lastDates = kwargs["last"]

        # 定义用户
        authors = ["anhengxuan", "chenfujun", "huangjiangwei"]
        authorsCommits = {}
        for authorName in authors:
            authorsCommits[authorName] = []

        now = datetime.datetime.now()
        # 从上周一开始算
        # lastWeekStart = now - timedelta(days=now.weekday() + int(lastDates))
        # 最近7天
        lastWeekStart = now - timedelta(int(lastDates))
        lastWeek = lastWeekStart.strftime("%Y-%m-%d 00:00")
        print(lastWeek)

        # 设置环境变量
        os.environ['dateInfo'] = lastWeek

        # 跳转目录
        workDir = os.getcwd()
        os.chdir(repoDir)
        print(os.getcwd())

        # 获取repo commit信息
        shellRet = Shell("repo", "forall", "-c", 'echo "# `basename $PWD`";echo;echo "commit id | date | author name | module | commit log";echo "---|---|---|---|---";git log --after "${dateInfo}" --pretty=format:"%H | %ai | %ae | `basename $PWD` | %s";echo;echo;echo;')

        # 跳转目录
        os.chdir(workDir)
        print(os.getcwd())

        # open BOM output file
        outputBomXL = xlwt.Workbook()
        style = xlwt.XFStyle()
        style.alignment.wrap = 1
        style.alignment.vert = xlwt.Alignment.VERT_CENTER
        style.alignment.horz = xlwt.Alignment.HORZ_CENTER

        leftStyle = xlwt.XFStyle()
        leftStyle.alignment.wrap = 1
        leftStyle.alignment.vert = xlwt.Alignment.VERT_CENTER
        leftStyle.alignment.horz = xlwt.Alignment.HORZ_LEFT

        currentSheet = None
        commitStart = False
        gitRepoStart = False
        row = 1
        for line in shellRet.splitlines():

            if line.startswith("# "):
                gitRepoStart = False
                commitStart = False
                name = line.strip()[2:]

                if not name in ["kernel-4.19", "device", "vendor"]:
                    continue

                # add same sheet[0] name to output file from input file sheet[0] name, we just analyze sheet[0]
                currentSheet = outputBomXL.add_sheet(name, cell_overwrite_ok = True)
                currentSheet.write(0, 0, "Commit id", style)
                currentSheet.col(0).width = 256 * 40
                currentSheet.write(0, 1, "date", style)
                currentSheet.col(1).width = 256 * 30
                currentSheet.write(0, 2, "author name", style)
                currentSheet.col(2).width = 256 * 30
                currentSheet.write(0, 3, "module", style)
                currentSheet.col(3).width = 256 * 10
                currentSheet.write(0, 4, "commit log", style)
                currentSheet.col(4).width = 256 * 120

                row = 1
                gitRepoStart = True
            elif gitRepoStart and "---|---|---|---|---" in line:
                commitStart = True
            elif commitStart and " | " in line:
                commitInfo = line.split(" | ")
                if len(commitInfo) == 5:

                    for col in range(5):
                        currentSheet.write(row, col, commitInfo[col], leftStyle)
                        currentSheet.row(row).height = 20 * 40

                    authorName = commitInfo[2].split("@")[0]
                    if authorName in authors:
                        authorsCommits[authorName].append(commitInfo)

                    row += 1
            else:
                pass

        outputBomXL.save("output/week_commit_" + datetime.datetime.now().strftime("%Y%m%d%H%M%S") + ".xls")

        # open BOM output file
        outputBomXL = xlwt.Workbook()
        style = xlwt.XFStyle()
        style.alignment.wrap = 1
        style.alignment.vert = xlwt.Alignment.VERT_CENTER
        style.alignment.horz = xlwt.Alignment.HORZ_CENTER

        leftStyle = xlwt.XFStyle()
        leftStyle.alignment.wrap = 1
        leftStyle.alignment.vert = xlwt.Alignment.VERT_CENTER
        leftStyle.alignment.horz = xlwt.Alignment.HORZ_LEFT

        for authorName in authors:
            # add same sheet[0] name to output file from input file sheet[0] name, we just analyze sheet[0]
            currentSheet = outputBomXL.add_sheet(authorName, cell_overwrite_ok = True)
            currentSheet.write(0, 0, "Commit id", style)
            currentSheet.col(0).width = 256 * 40
            currentSheet.write(0, 1, "date", style)
            currentSheet.col(1).width = 256 * 30
            currentSheet.write(0, 2, "author name", style)
            currentSheet.col(2).width = 256 * 30
            currentSheet.write(0, 3, "module", style)
            currentSheet.col(3).width = 256 * 10
            currentSheet.write(0, 4, "commit log", style)
            currentSheet.col(4).width = 256 * 120

            row = 1
            for commitInfo in authorsCommits[authorName]:
                for col in range(5):
                    currentSheet.write(row, col, commitInfo[col], leftStyle)
                    currentSheet.row(row).height = 20 * 40

                row += 1

        outputBomXL.save("output/week_bsp_" + datetime.datetime.now().strftime("%Y%m%d%H%M%S") + ".xls")

if __name__ == '__main__':
    RepoExcel({"dir": "../../android/merge/", "last": 120})


import torch
import numpy as np
import torch.nn as nn
import torch.nn.functional as F

class CNN(nn.Module):
    def __init__(self, vocab_size, embedding_dim, pre_trained_enbedding, filter_sizes, num_filters, dropout, output_size):
        super(CNN, self).__init__()
        # filter_sizes = [1,2,3,4,5]
        # num_filters = 20
        self.word_embeddings = nn.Embedding(vocab_size, embedding_dim)
        self.word_embeddings.weight.data.copy_(pre_trained_enbedding)
        self.word_embeddings.weight.requires_grad = False

        self.convs1 = nn.ModuleList([nn.Conv2d(1, num_filters, (K, embedding_dim)) for K in filter_sizes])
        self.dropout = nn.Dropout(dropout)
        self.fc1 = nn.Linear(len(filter_sizes)*num_filters, output_size)

    def forward(self,inputs):
        embeds = self.word_embeddings(inputs)
        x = embeds.view(embeds.size(0), 1, embeds.size(1), embeds.size(2))
        x = [F.relu(conv(x)).squeeze(3) for conv in self.convs1]
        x = [F.max_pool1d(i, i.size(2)).squeeze(2) for i in x]
        x = torch.cat(x, 1)
        x = self.dropout(x)
        out = self.fc1(x)
        probs = F.log_softmax(out, dim=1)

        return probs

Sentiment Analysis Deep Learning/model/TextCNN.py


import requests
from bs4 import BeautifulSoup
import pandas as pd
from sqlalchemy import create_engine

def get_html(url):
    r = requests.get(url)
    if r.status_code == 200:
        return r.text
        print(r.text)
    if r.status_code == 404:
        print('Страница не существует!')

def get_date(html):
    soup = BeautifulSoup(html, 'lxml')
    try:
        data_date = [p.text.strip()
                     for p in soup.find_all('td')if p.text is not None]
    except Exception.DoesNotExist:
        data_date = ''
    return data_date

def find_archive(page_archive):
    soup = BeautifulSoup(page_archive, 'lxml')
    try:
        data_arc = [p.text.replace('↓', '\n').strip()
                    for p in soup.find_all('td') if p.text is not None]
    except Exception.DoesNotExist:
        data_arc = ''
    return data_arc

def find_right(page_right):
    soup = BeautifulSoup(page_right, 'lxml')
    try:
        data_right = [p.text.replace('↓', '\n').strip()
                      for p in soup.find_all('table', {'class': 'items'})
                      if p.text is not None]
    except Exception.DoesNotExist:
        data_right = ''
    return data_right

def parse_table(table):
    # парсинг со страницы "реестр членов СРО" из 2 по 6 столбца информации
    res = pd.DataFrame()
    name_org = ''
    inn = ''
    ogrn = ''
    status = ''
    type = ''
    reg_num = ''

    data_tr = table.find('tr', {'class': 'sro-link'})
    name_org = data_tr.find_all('td')[1].text.strip()
    inn = data_tr.find_all('td')[2].text.strip()
    ogrn = data_tr.find_all('td')[3].text.strip()
    status = data_tr.find_all('td')[4].text.strip()
    type = data_tr.find_all('td')[5].text.strip()
    reg_num = data_tr.find_all('td')[6].text.strip()

    res = res.append(pd.DataFrame([[
        name_org, inn, ogrn, status, type,
        reg_num]],
        columns=[
        'name_org', 'inn', 'ogrn', 'status',
        'type', 'reg_num']))
    return res

def parse_page_2(page_2):
    # парсинг со страницы "Сведения о члене СРО" из 7 по 8 столбца информации
    res = pd.DataFrame()
    date_reg = ''
    date_close = ''

    date_reg = page_2[6]
    date_close_td = page_2[7]
    if date_close_td is not None:
        date_close = date_close_td

    res = res.append(pd.DataFrame(
        [[date_reg, date_close]],
        columns=['date_reg', 'date_close']), ignore_index=True)
    return res

def parse_page_arch(page_arch):
    # парсинг со страницы "Архив"
    res = pd.DataFrame()
    num_sv = ''
    date_open = ''
    basis = ''
    status_sv = ''

    num_sv = page_arch[7]
    date_open_td = page_arch[8]
    basis = page_arch[9]
    status_sv = page_arch[11]
    if date_open_td is not None:
        date_open = date_open_td

    res = res.append(pd.DataFrame([[
        num_sv, date_open, basis, status_sv]],
        columns=[
            'num_sv', 'date_open', 'basis', 'status_sv']), ignore_index=True)
    return res

def parse_page_right(page_right):
    # парсинг со страницы "Сведения о наличии права"
    res = pd.DataFrame()
    inform_of_the_right = ''
    inform_right = page_right[0]
    if inform_right is not None:
        inform_of_the_right = inform_right
    res = res.append(
        pd.DataFrame([[inform_of_the_right]], columns=['inform_of_the_right']))
    return res

def main():
    for i in num_inn:
        url_inn = base_url+(rel_inn)+i
        soup = BeautifulSoup(get_html(url_inn), 'lxml')
        tables = soup.find_all('table', {'class': 'items'})
        rel_cl = [tag.get('rel')for tag in soup.find_all('tr')
                  if not tag.get('rel') is None]
        num_clienta = rel_cl[0]
        url_client = base_url + num_clienta
        html = get_html(url_client)
        url_arch = url_client + '/certificates'
        url_of_the_right = url_client + '/rights'
        page_get_right = get_html(url_of_the_right)
        page_archive = get_html(url_arch)
        for item in tables:
            res = parse_table(item)
        data_date = parse_page_2(get_date(html))
        archive = parse_page_arch(find_archive(page_archive))
        the_right = parse_page_right(find_right(page_get_right))
        result = pd.DataFrame()
        df = pd.concat([res, data_date, archive, the_right], axis=1)
        result = result.append(df, ignore_index=True)
        engine = create_engine('sqlite:///pars_data.db')
        result.to_sql('users', con=engine, if_exists='append')

if __name__ == '__main__':
    base_url = 'http://reestr.nostroy.ru'
    rel_inn = '/reestr?m.fulldescription=&m.shortdescription=&m.inn='
    print("\n---------\n")
    num_inn = input("Введите ваш номер ИНН или список через пробел: ").split()
    # 0278128540 770965012811
    main()
    print("\n---------\n")


"""Split domain IDs into training/validation/test subsets."""

import os
import logging

from tfold_se3.utils import tfold_init
from tfold_se3.utils import get_md5sum

def export_chain_ids(chain_ids, path):
    """Export chain IDs to a plain-text file."""

    os.makedirs(os.path.dirname(os.path.realpath(path)), exist_ok=True)
    with open(path, 'w') as o_file:
        o_file.write('\n'.join(sorted(chain_ids)) + '\n')

def main():
    """Main entry."""

    # load configurations from the YAML file
    curr_dir = os.path.dirname(os.path.realpath(__file__))
    cfg_fpath = os.path.join(curr_dir, 'config_gpu241.yaml')
    config = tfold_init(cfg_fpath)

    # get chain IDs
    with open(config['cid_fpath_all'], 'r') as i_file:
        chain_ids = [i_line.strip() for i_line in i_file]

    # split domain IDs into training/validation/test subsets
    n_chains = len(chain_ids)
    chain_ids.sort(key=get_md5sum)
    n_chains_trn = int(n_chains * config['ratio_trn'])
    n_chains_val = int(n_chains * config['ratio_val'])
    n_chains_tst = n_chains - n_chains_trn - n_chains_val
    logging.info('# of chains: %d (trn) / %d (val) / %d (tst)',
                 n_chains_trn, n_chains_val, n_chains_tst)
    chain_ids_trn = chain_ids[:n_chains_trn]
    chain_ids_val = chain_ids[n_chains_trn:-n_chains_tst]
    chain_ids_tst = chain_ids[-n_chains_tst:]

    # export domain IDs belonging to each subset to a plain-text file
    export_chain_ids(chain_ids_trn, config['cid_fpath_trn'])
    export_chain_ids(chain_ids_val, config['cid_fpath_val'])
    export_chain_ids(chain_ids_tst, config['cid_fpath_tst'])

if __name__ == '__main__':
    main()


from __future__ import annotations

from typing import cast, Union, TypeVar, Optional, Protocol
from collections.abc import Iterable

from time import perf_counter
import logging

from .utils import isclose

ObjectiveBound = Union[float, int]

Objective = TypeVar('Objective', bound=ObjectiveBound, covariant=True)
LocalMove = TypeVar('LocalMove')

class SolutionProtocol(Protocol[Objective, LocalMove]):
    def objective(self) -> Objective: ...
    def copy(self: Solution) -> Solution: ...
    def random_local_moves_wor(self) -> Iterable[LocalMove]: ...
    def objective_incr_local(self, lmove: LocalMove) -> Optional[Objective]: ...
    def step(self, lmove: LocalMove) -> None: ...
    def perturb(self, ks: int) -> None: ...

Solution = TypeVar('Solution', bound=SolutionProtocol)

def ils(solution: Solution, budget: float, ks: int = 3) -> Solution:
    start = perf_counter()
    best = solution.copy()
    best_obj = cast(ObjectiveBound, best.objective())
    while perf_counter() - start < budget:
        # for move in solution.enumLocalMove():
        for move in solution.random_local_moves_wor():
            incr = cast(ObjectiveBound, solution.objective_incr_local(move))
            if incr < 0 and not isclose(incr, 0):
                solution.step(move)
                break
            if perf_counter() - start >= budget:
                obj = cast(ObjectiveBound, solution.objective())
                if obj < best_obj:
                    return solution
                else:
                    return best
        else:
            # Local optimum found
            obj = cast(ObjectiveBound, solution.objective())
            if obj < best_obj or isclose(obj, best_obj):
                best = solution.copy()
                best_obj = obj
                logging.info(f"New best solution found: {best_obj}")
            else:
                solution = best.copy()
            solution.perturb(ks)
    obj = cast(ObjectiveBound, solution.objective())
    if obj < best_obj:
        return solution
    else:
        return best


# -*- coding: utf-8 -*-
#harvest theses of university santacatrina
#JH: 2023-01-01

import re
from requests import Session
from time import sleep
from bs4 import BeautifulSoup
from typing import Union
import ejlmod3

publisher = 'Santa Catarina U.'
jnlfilename = 'THESES-SANTACATARINA-%s' % ejlmod3.stampoftoday()

rpp = 10
boring = ['Dissertação (Mestrado)', 'Dissertação (Mestrado profissional)',
          'Master Thesis']

def dspace_extract_links(content: str, prafix: str):
    soup = BeautifulSoup(content, 'lxml')
    article_links = soup.find_all('a', {'href': re.compile('/handle/\d{0,}/\d{0,}')})
    filtered = filter(lambda article: False if article.parent.get('class') is None
                        else True if 'artifact-title' in article.parent.get('class')
                        else False, article_links)

    return list(filtered)

def dspace_table_extract_information(titles: list, link: str, mapping: dict) -> Union[dict, int]:
    extracted_data: dict = {}

    with Session() as session:
        resp = session.get(link)

        if resp.status_code != 200:
            return -1

        resp_data = resp.content.decode('utf-8').replace('<?xml version="1.0" encoding="UTF-8"?>', '')
        soup = BeautifulSoup(resp_data, 'lxml')

        for title in titles:
            patter: str = '^%s$' % title.replace('.', '\\.')
            mapped_name: Union[str, dict] = mapping[title]

            if mapped_name is None:
                return -2

            fields = soup.find_all('td', text=re.compile(patter, re.MULTILINE))

            if len(fields) == 0:
                print("Can't find:", mapped_name)
                continue

            for field in fields:
                data_field: str = field.parent.find_all('td')[1].text

                datatype: int = mapping[title].get('datatype')

                if datatype == 1:
                    extracted_data[mapped_name.get('name')] = data_field
                elif datatype == 2:
                    if mapped_name.get('name') in extracted_data.keys():
                        extracted_data[mapped_name.get('name')].append(data_field)
                    else:
                        extracted_data[mapped_name.get('name')] = [data_field]
                elif datatype == 3:
                    if mapped_name.get('name') in extracted_data.keys():
                        extracted_data[mapped_name.get('name')].append([data_field])
                    else:
                        extracted_data[mapped_name.get('name')] = [[data_field]]

    # Get the pdf link
    pdf_section = soup.find_all('a', attrs={'href': re.compile('bitstream')})
    if len(pdf_section) == 2:
        rec['hidden'] = 'https://repositorio.ufsc.br%s' % pdf_section[0].get('href')

    return extracted_data

recs = []
alllinks = []
with Session() as session:

    for (dep, fc, pages) in [('76362', 'c', 2), ('75302', '', 2), ('77533', '', 2), ('123199', 'm', 1),
                             ('80823', 'mc', 1), ('103512', 'm', 2), ('74675', '', 0)]:
        for page in range(pages):
            to_curl = 'https://repositorio.ufsc.br/handle/123456789/' + dep + '/browse?rpp=' + str(
                rpp) + '&sort_by=2&type=dateissued&offset=' + str(rpp * page) + '&etal=-1&order=DESC'
            ejlmod3.printprogress('=', [[dep], [page+1, pages], [to_curl]])

            print('#### OPENING INDEX PAGE (%i/%i) OF %s:%s ####' % (page + 1, pages, dep, fc))
            index_resp = session.get(to_curl)

            if index_resp.status_code != 200:
                print('Error: Can\'t open the page\nSkipping this index page.')
                continue

            index_resp_data = index_resp.content.decode('utf-8').replace('<?xml version="1.0" encoding="UTF-8"?>', '')

            soup = BeautifulSoup(index_resp_data, 'lxml')
            article_links = dspace_extract_links(index_resp_data, 'https://repositorio.ufsc.br')
            rec = {}

            # Open the sub site
            for article in article_links:
                if ejlmod3.checkinterestingDOI(article.get('href')) and not article.get('href') in alllinks:
                    print('[%s] --> Harvesting data' % 'https://repositorio.ufsc.br%s?show=full' % article.get('href'))
                    alllinks.append(article.get('href'))
                else:
                    print('[%s]' % 'https://repositorio.ufsc.br%s?show=full' % article.get('href'))
                    continue
                try:
                    data_resp = session.get('https://repositorio.ufsc.br%s?show=full' % article.get('href'))
                except Exception:
                    print('[%s] --> Is not a real record. Skipping...' % 'https://repositorio.ufsc.br%s?show=full' %
                          article.get('href'))
                    continue

                if data_resp.status_code != 200:
                    print('[%s] Error: Can\'t open page!')
                    continue

                data_resp_data = data_resp.content.decode('utf-8').replace('<?xml version="1.0" encoding="UTF-8"?>', '')
                data_soup = BeautifulSoup(data_resp_data, 'lxml')

                mapping = {
                    'dc.contributor.advisor': {
                        'name': 'supervisor',
                        'datatype': 3
                    },
                    'dc.type': {
                        'name': 'note',
                        'datatype': 2
                    },
                    'dc.contributor.author': {
                        'name': 'autaff',
                        'datatype': 3
                    },
                    'dc.date.issued': {
                        'name': 'date',
                        'datatype': 1
                    },
                    'dc.identifier.uri': {
                        'name': 'link',
                        'datatype': 1
                    },
                    'dc.description.abstract': {
                        'name': 'abs',
                        'datatype': 1
                    },
                    'dc.subject.classification': {
                        'name': 'keyw',
                        'datatype': 2
                    },
                    'dc.title': {
                        'name': 'tit',
                        'datatype': 1
                    }
                }

                keepit = True
                rec = dspace_table_extract_information(mapping.keys(), 'https://repositorio.ufsc.br%s?show=full' % article.get('href'), mapping)
                rec['tc'] = 'T'
                rec['jnl'] = 'BOOK'
                if len(fc) != 0:
                    rec['fc'] = fc
                ejlmod3.metatagcheck(rec, data_soup, ['DCTERMS.extent', 'DC.language', 'DCTERMS.abstract', 'DC.type'])
                rec['autaff'][-1].append(publisher)
                if 'note' in rec:
                    for note in rec['note']:
                        if note in boring:
                            keepit = False
                            ejlmod3.adduninterestingDOI(article.get('href'))
                if keepit:
                    recs.append(rec)
                sleep(5)
            sleep(10)
        sleep(5)

ejlmod3.writenewXML(recs, publisher, jnlfilename)


from flask import Flask, request, jsonify
from flask_sock import Sock
import pyaudio

app = Flask(__name__)
sock = Sock(app)

# test websocket endpoint
@sock.route('/api/test', methods=['GET'])
def test(ws):
    while True:
        ws.send("hello world")

####################################################################

# detection endpoints
@app.route('/api/detection', methods=['GET'])
def detactions_options():
    return '***'

@sock.route('/api/detection/audio', methods=['GET'])
def get_audio(ws):
    audio_format = pyaudio.paInt16

    channels = 1
    sample_rate = 44100
    chunk_size = 512

    audio = pyaudio.PyAudio()

    stream = audio.open(format=audio_format, channels=channels, rate=sample_rate, input=True, frames_per_buffer=chunk_size, input_device_index=0)

    try:
        while True:
            data = stream.read(1024)
            print(data)
            print('_________________________________________')
            ws.send(data)

    except KeyboardInterrupt:
            pass
    stream.stop_stream()
    stream.close()
    audio.terminate()

@app.route('/api/detection/detectShot', methods=['POST'])
def detect_shot():
    if 'file' not in request.files:
        return 'No file part in the request'

    file = request.files['file']

    if file.filename == '':
        return 'No selected file'

    # send file to shot detector

    # get return and return

    resp = {
        'shot':True
    }

    return jsonify(resp)

@app.route('/api/detection/getLocation', methods=['POST'])
def get_location():
    if 'file' not in request.files:
        return 'No file part in the request'

    file = request.files['file']

    if file.filename == '':
        return 'No selected file'

    # noice redection

    # send file to audio madel

    # return value

    return 'File uploaded successfully'

# drone endpoints
@app.route('/api/drone', methods=['GET'])
def drone_options():
    return 'drone_options'

@app.route('/api/drone/chooseDrone', methods=['POST'])
def choose_drone():
    return 'choose_drone'

@app.route('/api/drone/sendDrone', methods=['POST'])
def send_drone():
    return 'send_drone'

# TOTO CV endpoints
@sock.route('/api/toto', methods=['GET'])
def toto_options(ws):
    while True:
        data = ws.receive()
        ws.send(data[::-1])
    return 'drone_options'

@app.route('/api/toto/getObjects', methods=['POST'])
def get_objects():
    return 'get_objects'

# surge endpoints
@app.route('/api/surge', methods=['GET'])
def surge_options():
    return 'surge_options'

# cloud endpoints

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


import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit

# Parameters
b_n = [0.1, 1, 10]
d_n = [0.2, 2, 5]
dt = 0.01
max_it = 100000
td_list = []
tb_list = []

fig, axs = plt.subplots(nrows=len(b_n), ncols=2, figsize=(10, 6))
for j, (b, d) in enumerate(zip(b_n, d_n)):
    td_data = np.zeros(max_it)
    tb_data = np.zeros(max_it)
    for i in range(max_it):
        t = 0
        td = 0
        tb = 0
        while tb == 0 or td == 0:
            t += dt
            r = np.random.rand()
            if tb == 0 and r < b*dt:
                tb = t
                tb_data[i] = tb
            if td == 0 and r < d*dt:
                td = t
                td_data[i] = td

    tb_data = tb_data[tb_data != 0]
    td_data = td_data[td_data != 0]
    tb_list.append(tb_data)
    td_list.append(td_data)

    # Plot distribution of tb
    ax = axs[j, 0]
    tb_hist, tb_edges = np.histogram(tb_list[j], bins=100, density=True)
    # tb_centers = (tb_edges[:-1] + tb_edges[1:]) / 2
    ax.hist(tb_edges[:-1], tb_edges, weights=tb_hist, density=True, color='#91413f', edgecolor='k')
    ax.plot(tb_list[j], np.exp(-b_n[j] * np.array(tb_list[j])), 'b-')
    ax.set_yscale('log')
    ax.set_xlabel(r'$t_b$')
    ax.set_ylabel(r'log($P(t_b)$)')
    ax.set_title(fr'$b_n$={b}')

    # Plot distribution of td
    ax = axs[j, 1]
    td_hist, td_edges = np.histogram(td_list[j], bins=100, density=True)
    # td_centers = (td_edges[:-1] + td_edges[1:]) / 2
    ax.hist(td_edges[:-1], td_edges, weights=td_hist, density=True, color='#21d18d', edgecolor='k')
    ax.plot(td_list[j], np.exp(-d_n[j] * np.array(td_list[j])), 'b-')
    ax.set_yscale('log')
    ax.set_xlabel(r'$t_d$')
    ax.set_ylabel(r'log($P(t_d)$)')
    ax.set_title(fr'$d_n$={d}')

fig.tight_layout()
plt.show()

def exp_fit(x, y):
    def f(x, A, lambda_):
        return A * np.exp(-lambda_ * x)

    popt, pcov = curve_fit(f, x, y)

    return popt[1], pcov[1, 1]

# Fit exponential function to infection data
for j, (b, d) in enumerate(zip(b_n, d_n)):
    tb_data_j = tb_list[j][tb_list[j] > 0]
    hist, bin_edges = np.histogram(tb_data_j, bins=100, density=True)
    bin_centers = (bin_edges[:-1] + bin_edges[1:]) / 2
    lambda_i, cov_i = exp_fit(bin_centers, hist)
    print(f'For b = {b}, lambda_inf = {lambda_i:.3f}')

# Fit exponential function to recovery data
for j, (b, d) in enumerate(zip(b_n, d_n)):
    td_data_j = td_list[j][td_list[j] > 0]
    hist, bin_edges = np.histogram(td_data_j, bins=100, density=True)
    bin_centers = (bin_edges[:-1] + bin_edges[1:]) / 2
    lambda_r, cov_r = exp_fit(bin_centers, hist)
    print(f'd = {d}, lambda_rec = {lambda_r:.3f}')

for i in range(len(tb_list)):
    np.save(f'td_arr{i}', td_list[i])
    np.save(f'tb_arr{i}', tb_list[i])

for i in range(len(tb_list)):
    print('b = {}, <t_b> = {}'.format(b_n[i], np.mean(tb_list[i])))
    print('d = {}, <t_d> = {}'.format(d_n[i], np.mean(td_list[i])))


# @File    : base_8
# @Author  : yangbaihua
# @Time    : 2020/6/11 9:58

import random

def WhileDemo():
    count = 1
    randomNumber1 = 0
    randomNumber2 = 0
    #两个骰子不等于6会一直循环
    while not (randomNumber1 == 6 and randomNumber2 == 6):
        randomNumber1 = random.randint(1,6)
        randomNumber2 = random.randint(1,6)

        total = randomNumber1 + randomNumber2
        print("第",count,"次打印两个骰子的和为：",total)
        #首先判断两个数是否相等的
        if randomNumber1 == randomNumber2:
            print("第",count,"次Double")
            break
        else:
            #如果不相等，在判断两个数之和的大小
            if total > 8:
                print("第",count,"次Big")
            elif total > 4:
                print("第",count,"次Not Bad")
            else:
                print("第",count,"次Small")
        count+=1
if __name__ == '__main__':
    WhileDemo()


"""Provide a bunch of transformation

A segment is defined as 2 points (tuples)
We consider segments as oriented, so the order of these points matter
"""
import numpy as np

ORIGIN = np.array([0, 0])

def get_angle(v1, v2, as_radian=False):
    """Return angle b/w 2 angles"""
    angle = np.math.atan2(np.linalg.det([v1, v2]), np.dot(v1, v2))
    return angle if as_radian else np.degrees(angle)

def get_length(vector):
    """Return length of vector"""
    return np.linalg.norm(vector)

def to_array(arr):
    """Convert a to arr if not numpy array"""
    return arr if isinstance(arr, np.ndarray) else np.array(arr)

# --------------------------------------------------------------------------- #
# ---- Transformations

def get_params(seg1, seg2, as_radian=False):
    """Return parameters to transform seg1 into seg2"""
    seg1, seg2 = to_array(seg1), to_array(seg2)
    p11, p12 = seg1[0], seg1[1]
    p21, p22 = seg2[0], seg2[1]
    v1 = p12 - p11
    v2 = p22 - p21
    return {
        'angle': get_angle(v1, v2, as_radian=as_radian),
        'factor': get_length(v2) / get_length(v1),
        'vector': p21 - p11,
        'as_radian': as_radian,
        'origin': p11,
    }

def rot_matrix(angle, as_radian=False):
    """Return 2d rotation matrix"""
    if not as_radian:
        angle = np.radians(angle)
    cos, sin = np.cos(angle), np.sin(angle)
    return np.array([[cos, -sin], [sin, cos]])

def rotate(points, angle, origin=ORIGIN, as_radian=False):
    """Rotate points by angle relative to origin

    Args:
        points (matrix) : 2d-matrix (n*2) of points
        angle (float)   : angle of rotation (in degrees)
        origin (2-float array): point to rotate relative to
        as_radian (bool): if angle is given in radian, not degrees

    Return:
        (matrix): 2d-matrix (n*2) of rotated points
    """
    translation = np.transpose([origin])
    rotation = rot_matrix(angle, as_radian=as_radian)
    points_t = np.transpose(points)
    return np.transpose(np.dot(rotation, points_t - translation) + translation)

def scale(points, factor, origin=ORIGIN):
    """Apply hometheties b/w points and origin"""
    translation = to_array(origin)
    points = to_array(points)
    return factor * (points - translation) + translation

def transform(points, angle=None, factor=None, vector=None, origin=ORIGIN,
              as_radian=False, decimals=3):
    """Full transformation on points (rotation, homothety and translation)

    Args:
        points (matrix) : 2d-matrix (n*2) of points
        angle (float)   : angle of rotation (in degrees)
        factor (float)  : homothety factor (distance scaling)
        vector (2-float array)  : translation after other transformations
        origin (2-float array)  : origin of transformations
        as_radian (bool): if angle is given in radian, not degrees
        decimals (int)  : round transformed points
    """
    assert not (factor is None and vector is None and angle is None), (
        "Expecting at least one transformation"
    )
    points = to_array(points)
    if angle is not None:
        points = rotate(points, angle, origin=origin, as_radian=as_radian)
    if factor is not None:
        points = scale(points, factor, origin=origin)
    if vector is not None:
        vector = to_array(vector)
        points += vector
    if decimals and len(points):
        points = np.round(points, decimals)
    return points


# -*- coding: utf-8 -*-
# author: zhouwei
# description: 仅适用于通过key连接，如果需要密码连接，需要修改本脚本的内容
import paramiko as paramiko

class SSHClient:
    def __init__(self, host, port, user, pkey):
        self.ssh_host = host
        self.ssh_port = port
        self.ssh_user = user
        self.private_key = paramiko.RSAKey.from_private_key_file(pkey)
        self.ssh = None
        self._connect()

    def _connect(self):
        self.ssh = paramiko.SSHClient()
        self.ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        try:
            self.ssh.connect(hostname=self.ssh_host, port=self.ssh_port, username=self.ssh_user, pkey=self.private_key,
                             timeout=10)
        except:
            return 'ssh connect fail'

    def execute_command(self, command):
        stdin, stdout, stderr = self.ssh.exec_command(command)
        out = stdout.read()
        err = stderr.read()
        return out, err

    def close(self):
        self.ssh.close()


# !/usr/bin/python
# coding=utf-8

from datetime import datetime
from .. import rd
from .. import util

'''
1. 文章详细信息; 使用redis中的散列类型保存; key是'post:id'; 字段包括: title author time content category;
2. 文章总数; 保存于posts:count中; 只增不减;
3. 文章ID列表; 使用列表类型posts:list记录文章列表;
    a. 新文章发布使用LPUSH将文章加入到列表中;
    b. 删除文章 LREM
    c. 文章分页显示 LRANGE
4. 用户文章列表; 保存一个用户的所有文章; 用户名对应一个POST ID列表; posts:author
4. 删除文章列表; 使用列表类型保存 posts:del:list
'''

# 文章列表
POSTS_LIST = 'posts:list'
# 文章总数
POSTS_COUNT = 'posts:count'
# 文章删除列表
POSTS_DEL_LIST = 'posts:del_list'
# 每个作者文章列表
POST_AUTHOR_LIST = 'posts:author:'
# post infomation
POST_INFO = 'post:'

def publish_post(title, author_id, content, category):
    """
    publish post
    """
    post_id = rd.incr(POSTS_COUNT)
    pipe = rd.pipeline()
    pipe.multi()
    rd.hmset(POST_INFO + '{}'.format(post_id), {'post_id': post_id, 'title': title, 'author_id': author_id,
                                                'content': content, 'category': category, 'time': datetime.utcnow()})
    rd.lpush(POSTS_LIST, post_id)
    rd.lpush(POST_AUTHOR_LIST + '{}'.format(author_id), post_id)
    pipe.execute()

def update_post_content(post_id, content):
    """
     update post content
    """
    return rd.hset(POST_INFO + '{}'.format(post_id), 'content', content)

def delete_post(post_id):
    """
    delete post
    """
    pipe = rd.pipeline()
    pipe.multi()
    rd.lrem(POSTS_LIST, post_id, 0)
    rd.lpush(POSTS_DEL_LIST, post_id)
    pipe.execute()

def total_posts():
    """
    return total posts number
    """
    return rd.llen(POSTS_LIST)

def total_posts_by_author(author_id):
    """
    return total posts number of author
    """
    return rd.llen(POST_AUTHOR_LIST + '{}'.format(author_id))

def posts_by_page(page_id, per_page):
    """
    paging display
    page_id: start from 1
    post ID list key is str type
    """
    pages = int(rd.llen(POSTS_LIST) / per_page + 1)
    if 0 < page_id <= pages:
        return util.convert(rd.lrange(POSTS_LIST, (page_id - 1) * per_page, page_id * per_page - 1))
    print('invaild param page id[{0}, {1}]'.format(1, pages))

def get_post(post_id):
    """
    get post infomation
    return dict
    """
    return util.convert(rd.hgetall(POST_INFO + '{}'.format(post_id)))

def posts_by_author(author_id, page_id, per_page):
    """
    paging display
    get posts list by author
    """
    pages = int(rd.llen(POST_AUTHOR_LIST + '{}'.format(author_id)) / per_page + 1)
    if 0 < page_id <= pages:
        return util.convert(rd.lrange(POST_AUTHOR_LIST + '{}'.format(author_id), (page_id - 1) * per_page,
                                      page_id * per_page - 1))
    print('posts_by_author invaild param author {0} page id[{1}, {2}]: {3}'.format(author_id, 1, pages, page_id))


import pygame
import sys

from bullet import Bullet
from baddie import Baddie
import time

def check_events_whilst_inactive(player, bullets, baddies, stats, play_button):
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()
        if event.type == pygame.MOUSEBUTTONDOWN:
            mouse_x, mouse_y = pygame.mouse.get_pos()
            check_play_button(player, bullets, baddies, stats, play_button, mouse_x, mouse_y)

def check_events(player, screen, settings, bullets, baddies, audio, ADDBADDIE, stats, level_up_msg):
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()
        if event.type == pygame.KEYDOWN:
            check_keydown_events(event, player, screen, settings, bullets, baddies, audio, stats, level_up_msg)
        if event.type == pygame.KEYUP:
            check_keyup_events(event, player)
        if event.type == ADDBADDIE:
            create_baddie(screen, baddies, settings, audio)

def check_keydown_events(event, player, screen, settings, bullets, baddies, audio, stats, level_up_msg):
    if event.key == pygame.K_RIGHT:
        player.moving_right = True
    if event.key == pygame.K_LEFT:
        player.moving_left = True
    if event.key == pygame.K_UP:
        player.moving_up = True
    if event.key == pygame.K_DOWN:
        player.moving_down = True
    if event.key == pygame.K_SPACE:
        fire_bullet(player, screen, settings, bullets, audio)
    if event.key == pygame.K_b:
        create_baddie(screen, baddies, settings, audio)
    if event.key == pygame.K_l:
        level_up(stats, audio, level_up_msg)

def check_keyup_events(event, player):
    if event.key == pygame.K_RIGHT:
        player.moving_right = False
    if event.key == pygame.K_LEFT:
        player.moving_left = False
    if event.key == pygame.K_UP:
        player.moving_up = False
    if event.key == pygame.K_DOWN:
        player.moving_down = False

def move(player):
    if player.moving_right:
        if player.rect.right <= player.screen_rect.right:
            player.rect.x += 1
    if player.moving_left:
        if player.rect.left >= player.screen_rect.left:
            player.rect.x -= 1
    if player.moving_up:
        if player.rect.top >= player.screen_rect.top:
            player.rect.y -= 1
    if player.moving_down:
        if player.rect.bottom <= player.screen_rect.bottom:
            player.rect.y += 1

def fire_bullet(player, screen, settings, bullets, audio):
    new_bullet = Bullet(player, screen, settings)
    if len(bullets) < settings.max_bullets:
        bullets.add(new_bullet)
        audio.shot.play()

def update_screen(player, screen, bullets, baddies, sb, stats, level_up_msg, settings):
    screen.fill((255, 255, 255))
    sb.prep_score()
    sb.show_score()
    check_if_levelled_up(stats, level_up_msg, settings)
    for bullet in bullets.sprites():
        bullet.draw_bullet()
    player.blitme()
    for baddie in baddies.sprites():
        baddie.blitme()
    pygame.display.flip()

def update_screen_whilst_inactive(screen, sb, stats, end_msg, play_button):
    screen.fill((255, 255, 255))
    sb.prep_score()
    sb.show_score()
    play_button.draw_button()
    if not stats.is_first_round:
        end_msg.prep_msg(play_button)
        end_msg.show_msg()
    pygame.display.flip()

def update_bullets(bullets):
    bullets.update()

    for bullet in bullets.copy():
        if bullet.rect.y <= 0:
            bullets.remove(bullet)

def check_bullet_collisions(baddies, bullets, stats, audio, settings, level_up_msg):
    collisions = pygame.sprite.groupcollide(baddies, bullets, True, True)
    if collisions:
        stats.score += 1
        # audio.baddie_ded.play()
        if stats.score > 0:
            if (stats.score % settings.baddies_per_round) == 0:
                level_up(stats, audio, level_up_msg)

def create_baddie(screen, baddies, settings, audio):
    new_baddie = Baddie(screen, settings)
    baddies.add(new_baddie)
    # audio.baddie_spawn.play()

def level_up(stats, audio, level_up_msg):
    stats.level += 1
    stats.baddie_drop_speed *= 1.1
    audio.play_trump_soundbite()
    time.sleep(1)

def check_if_levelled_up(stats, level_up_msg, settings):
    if stats.score > 0:
        if stats.score % settings.baddies_per_round == 0:
                display_level_up_msg(level_up_msg)

def display_level_up_msg(level_up_msg):
    level_up_msg.prep_level_up_msg()
    level_up_msg.show_level_up_msg()

def update_baddies(baddies, settings, stats):
    baddies.update(stats)
    for baddie in baddies.copy():
        if baddie.rect.y >= settings.screen_height:
            baddies.remove(baddie)

def check_baddie_player_collisions(baddies, player, stats, bullets, audio):
    if pygame.sprite.spritecollideany(player, baddies):
        life_lost(stats, player, bullets, baddies, audio)

def check_baddie_screen_bottom(screen, stats, player, bullets, baddies, audio):
    screen_rect = screen.get_rect()
    for baddie in baddies:
        if baddie.rect.bottom >= screen_rect.bottom:
            life_lost(stats, player, bullets, baddies, audio)

def life_lost(stats, player, bullets, baddies, audio):
    if stats.lives_remaining >= 1:
        stats.lives_remaining -= 1
    else:
        end_game(stats)
    audio.player_ded.play()
    time.sleep(0.2)
    reset_positions(player, bullets, baddies)

def reset_positions(player, bullets, baddies):
    player.reset_position()
    bullets.empty()
    baddies.empty()

def end_game(stats):
    stats.game_active = False

def check_play_button(player, bullets, baddies, stats, play_button, mouse_x, mouse_y):
    if play_button.rect.collidepoint(mouse_x, mouse_y):
        stats.reset_stats()
        reset_positions(player, bullets, baddies)
        stats.game_active = True

def no_longer_first_round(stats):
    stats.is_first_round = False


# encoding: UTF-8

'''
实现读取wind行情历史数据.mat格式
'''

import scipy.io as sio
import pandas as pd
import datetime, re, logging
from module_mylog import gLogger

class LoadMatFile(object):

    def __init__(self, matFile):
        self.matFile = matFile
        self.data = sio.loadmat(self.matFile)
        gLogger.info("load mat file completed")
        self.dfData = self.convert2df()

    def convert2df(self):
        try:
            colNames = ['vtSymbol','symbol','date','time','lastPrice','lastVolume','lastTurnover','matchItems','openInterest','tradeFlag','bsFlag','volume','turnover'
                        ,'highPrice','lowPrice','openPrice','preClosePrice','settlementPrice','position','curDelta','preSettlementPrice','prePosition','askPrice1'
                        ,'askVolume1','bidPrice1','bidVolume1','askAvPrice','bidAvPrice','totalAskVolume','totalBidVolume','index','stocks','ups','downs','holdLines']

            seriesNames = locals()
            for k,v in enumerate(self.data['temp'][0][0].tolist()):
                if colNames[k] in ['askPrice1','askVolume1','bidPrice1','bidVolume1']:
                    v = v[:,0]
                if v.ndim  == 1:
                    seriesNames['SN_%s' % k] = pd.Series(v, name = colNames[k])
                elif v.ndim > 1:
                    v = v.ravel()
                    seriesNames['SN_%s' % k] = pd.Series(v, name=colNames[k])
                else:
                    print ("index = %d, dim = %d" %(k, v.ndim))

            df_data = pd.concat([seriesNames[a] for a in seriesNames if 'SN_' in a], axis = 1)
            df_data = df_data.dropna(axis=0,how='any')
            dfData = self.normalizeData(df_data)
            return dfData
        except Exception as e:
            gLogger.exception("Exception when convert to df: %s" %e)

    def normalizeData(self, df):
        try:
            gLogger.info("start normalize df Data")
            f = lambda x:str(int(x))
            df["date"] = df["date"].map(f)
            df["time"] = df["time"].map(f)
            df["time"] = df["time"].map(lambda x: x.zfill(9))
            vtSymbol = self.matFile.split('\\')[-1].split('_')[0]
            symbol = "".join([a for a in vtSymbol if a.isalpha()]).lower()
            date = self.matFile.split('\\')[-1].split('_')[-1][:-4]
            df["vtSymbol"] = vtSymbol
            df["symbol"] = symbol
            df["DT"] = df["date"] + ' ' + df["time"]
            df["datetime"] = df["DT"].map(self.convert2datetime)
            del df["DT"]
            df["date"] = date
            return df
        except Exception as e:
            gLogger.exception("Exception when convert to df: %s" %e)

    def convert2datetime(self, target):
        try:
            return datetime.datetime.strptime(target, "%Y%m%d %H%M%S%f")
        except:
            gLogger.error("Error when convert to df: %s" % target)
            return None


from eom import Obj
from collision import Collision
import numpy as np
import threading
from animate_test import PlotPoly
import time as SysTime

class ForceArray():
    def __init__(self):
        self.ft = np.linspace(0,5,50)
        self.f_array = np.zeros([1, 50])
        self.f_array[0, 0] = 10

eom_obj = Obj()
plot_obj = PlotPoly()
itrs = 500
tf = 5.0
t0 = 0.0

def run():
    collision_obj = Collision()
    force_obj = ForceArray()
    eom_obj.set_force_array(force_obj)
    eom_obj.set_collision_check(True)
    eom_obj.store_trajectory(True)
    tspan = [t0, tf]
    dt = (tspan[1] - tspan[0]) / 500
    for i in range(1, itrs):
        [d, v] = eom_obj.update(dt)
        SysTime.sleep(dt)

    eom_obj.set_sim_finished(True)

def main():
    plot_obj.set_data_gen_fcn(eom_obj.get_pos_last())

    t = threading.Thread(target=run)
    t.start()

    plot_obj.create_plot()
    eom_obj.plot_trajectory('All')

if __name__ == '__main__':
    main()


# coding=utf-8
__author__ = 'arachis'
"""
Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left to right, level by level from leaf to root).
遍历一棵树，返回一个列表，以从底向上的顺序存储列表，每一层按从左到右的顺序，如[
  [15,7],
  [9,20],
  [3]
]是二叉树
    3
   / \
  9  20
    /  \
   15   7
的返回结果
"""

class Solution(object):
    """
    维护一个全局列表，存储每层元素
    维护一个变量，标识当前层数
    """
    def levelOrderBottom(self, root):
        if root == None: return []
        wrapList = [[]]
        self.levelMaker(wrapList, root, 0)
        return wrapList
    def levelMaker(self,list,root,level):#根据题意，按left,right,root的顺序遍历
        if(root == None): return
        if(level >= len(list) ) : list.insert(0,[]) #达到最后一层
        self.levelMaker(list, root.left, level+1) #update left subtree
        self.levelMaker(list, root.right, level+1) #update right subtree
        list[ len(list)-1-level ].append(root.val) #set list

OJ/107. Binary Tree Level Order Traversal II.py


import joblib
import numpy as np
from flask import Flask, jsonify, request
import joblib
import logging
from datetime import date
import random
import os
import pickle
import pandas as pd

# Get the current directory of the Python file
base_dir = os.path.dirname(os.path.relpath(__file__))

model_path = os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), 'model', 'logreg_model.joblib')
column_name_path = os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), 'model', 'col_names.pk1')

app = Flask(__name__)
app.logger.setLevel(logging.DEBUG)

# Load the KNN model
logreg = joblib.load(model_path)
with open(column_name_path, 'rb') as f:
    col_names = pickle.load(f)

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

    today = date.today()  # get today's date
    jan_1 = date(today.year, 1, 1)  # create a date object for January 1st of this year
    delta = today - jan_1  # calculate the time difference between January 1st and today
    days_since_jan_1 = delta.days + 1  # add 1 to include January 1st

    odd_or_even = days_since_jan_1%2

    if odd_or_even == 0:
        # Return the predicted activity as a JSON response
        activity = ['BubblePopActivity', 'YogaActivity', 'ColoringActivity', 'RainbowActivity', 'SelfHugActivity',
         'WaterDrinkingActivity']
        print("Inside If", days_since_jan_1)
        return jsonify({'activity': random.choice(activity)})

    else:
        # Get the emotion and feedback values from the request
        print("Inside else", days_since_jan_1)

        emotion = request.json['emotion']
        feedback = request.json['feedback']

        # Create a new DataFrame with a single row containing the encoded input values
        input_data = pd.DataFrame({'emotion_' + emotion: [1], 'feedback_' + feedback: [1]})

        # Add any missing columns to the input_data DataFrame
        missing_cols = set(col_names) - set(input_data.columns)
        for c in missing_cols:
            input_data[c] = 0

        # Reorder the columns in the input_data DataFrame to match the order of the columns in X
        input_data = input_data[col_names]

        prediction = logreg.predict(input_data)

        # Return the predicted activity as a JSON response
        return jsonify({'activity': prediction[0]})

# Test api for outside access tested.
@app.route('/predict/test', methods=['GET'])
def predicthi():
    return "Predict Till Test"

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=8080)
    # app.run(host='127.0.0.1', port=5000)


import dgl
import torch
import torch.nn as nn
import torch.nn.functional as F
from dgl.nn.pytorch import GATConv, GraphConv

class GAT(nn.Module):
    def __init__(
        self,
        g: dgl.DGLGraph,
        in_feats: int,
        hidden_feats: int,
        out_feats: int,
        num_layers: int,
        heads: list[int],
        activation: str,
        feats_dropout: float = 0,
        attention_dropout: float = 0,
        negative_slope: float = 0.2,
        residual: bool = False,
    ):
        super().__init__()
        self._g = g
        self._num_layers = num_layers

        if activation == 'relu':
            self._activation = nn.ReLU()
        elif activation == 'leaky_relu':
            self._activation = nn.LeakyReLU()

        self._layers = self._create_layers(
            in_feats,
            hidden_feats,
            out_feats,
            heads,
            feats_dropout,
            attention_dropout,
            negative_slope,
            residual,
        )

    def _create_layers(
        self,
        in_feats: int,
        hidden_feats: int,
        out_feats: int,
        heads: list[int],
        feats_dropout: float,
        attention_dropout: float,
        negative_slope: float,
        residual: bool,
    ) -> nn.ModuleList:
        layers = nn.ModuleList()

        layers.append(GATConv(
            in_feats,
            hidden_feats,
            heads[0],
            feats_dropout,
            attention_dropout,
            negative_slope,
            False,
            self._activation,
            allow_zero_in_degree=True,
        ))

        for i in range(1, self._num_layers):
            layers.append(GATConv(
                hidden_feats * heads[i - 1],
                hidden_feats,
                heads[i],
                feats_dropout,
                attention_dropout,
                negative_slope,
                residual,
                self._activation,
                allow_zero_in_degree=True,
            ))

        layers.append(GATConv(
            hidden_feats * heads[-2],
            out_feats,
            heads[-1],
            feats_dropout,
            attention_dropout,
            negative_slope,
            residual,
            None,
            allow_zero_in_degree=True,
        ))

        return layers

    def forward(self, inputs, get_attention: bool = False) -> list[torch.Tensor]:
        h = inputs

        for i in range(self._num_layers):
            h = self._layers[i](self._g, h).flatten(1)

        output_projection, attention = self._layers[-1](
            self._g, h, get_attention)
        logits = output_projection.mean(1)

        return logits, attention

class GCN(nn.Module):
    def __init__(
        self,
        g,
        in_feats,
        hidden_feats,
        out_feats,
        num_layers,
        activation,
        dropout,
    ):
        super().__init__()
        self._g = g

        if activation == 'relu':
            self._activation = nn.ReLU()
        elif activation == 'leaky_relu':
            self._activation = nn.LeakyReLU()

        self.layers = nn.ModuleList()
        # input layer
        self.layers.append(GraphConv(in_feats, hidden_feats, activation=self._activation, allow_zero_in_degree=True))
        # hidden layers
        for i in range(num_layers - 1):
            self.layers.append(GraphConv(hidden_feats, hidden_feats, activation=self._activation, allow_zero_in_degree=True))
        # output layer
        self.layers.append(GraphConv(hidden_feats, out_feats, allow_zero_in_degree=True))
        self.dropout = nn.Dropout(p=dropout)

    def forward(self, features):
        h = features
        for i, layer in enumerate(self.layers):
            if i != 0:
                h = self.dropout(h)
            h = layer(self._g, h)
        return h

def train(model, g, optimizer):
    pass

if __name__ == '__main__':
    dataset = dgl.data.CoraGraphDataset()
    g = dataset[0]

    g = dgl.remove_self_loop(g)
    g = dgl.add_self_loop(g)

    train_mask = g.ndata['train_mask']
    val_mask = g.ndata['val_mask']
    test_mask = g.ndata['test_mask']

    for round in range(300):
        model = GAT(
            g,
            in_feats=g.ndata['feat'].shape[-1],
            hidden_feats=8,
            out_feats=dataset.num_classes,
            num_layers=1,
            heads=[8, 1],
            activation='relu',
            feats_dropout=0,
            attention_dropout=0,
            negative_slope=0.2,
            residual=False,
        )

        optimizer = torch.optim.Adam(model.parameters())

        for epoch in range(200):
            model.train()
            optimizer.zero_grad()

            logits, attention = model(g.ndata['feat'], get_attention=True)

            loss = F.cross_entropy(
                logits[train_mask], g.ndata['label'][train_mask])

            loss.backward()
            optimizer.step()

            _, indices = torch.max(logits[train_mask], dim=1)
            correct = torch.sum(indices == g.ndata['label'][train_mask])
            accuracy = correct.item() / len(g.ndata['label'][train_mask])

        attention = attention.view(-1)
        criterium, _ = torch.kthvalue(
            attention, int(attention.shape[0] * 0.01))
        eids = torch.nonzero(attention <= criterium).view(-1)

        num_edges = g.num_edges()

        g = dgl.remove_edges(g, eids)

        print(f'GAT # Round: {round + 1} Loss: {loss:.2f} Accuracy: {accuracy * 100:.2f} % Num edges: {num_edges} Sparsity: {0.99 ** round * 100:.2f} %')

        model = GCN(
            g,
            in_feats=g.ndata['feat'].shape[-1],
            hidden_feats=16,
            out_feats=dataset.num_classes,
            num_layers=1,
            activation='relu',
            dropout=0.5,
        )

        optimizer = torch.optim.Adam(model.parameters())

        for epoch in range(200):
            model.train()
            optimizer.zero_grad()

            logits = model(g.ndata['feat'])

            loss = F.cross_entropy(
                logits[train_mask], g.ndata['label'][train_mask])

            loss.backward()
            optimizer.step()

            _, indices = torch.max(logits[train_mask], dim=1)
            correct = torch.sum(indices == g.ndata['label'][train_mask])
            accuracy = correct.item() / len(g.ndata['label'][train_mask])

        print(f'GCN # Round: {round + 1} Loss: {loss:.2f} Accuracy: {accuracy * 100:.2f} %')