import numpy as np
x = np.array([1,2,3])
print(np.exp(x))

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

print(sigmoid(x))

def sigmoid_derivative(x):
    s = 1/(1+np.exp(-x))
    ds = s*(1-s)
    return ds

print(sigmoid_derivative(x))

def image2vector(image):
    v = image.reshape((image.shape[0] * image.shape[1]*image.shape[2]),1)
    return v

def normalizeRows(x):
    x_norm = np.linalg.norm(x, axis=1, keepdims=True)
    return x/x_norm

x = np.array([[0,3,4],[1,6,4]])
print("normalizeRow(x) = " + str(normalizeRows(x)))

def softmax(x):
    x_exp = np.exp(x)
    x_sum = np.sum(x_exp, axis = 1, keepdims=True)
    s = x_exp/x_sum
    return s

print(softmax(x))

# vectorization
import time
x1 = [9, 2, 5, 0, 0, 7, 5, 0, 0, 0, 9, 2, 5, 0, 0]
x2 = [9, 2, 2, 9, 0, 9, 2, 5, 0, 0, 9, 2, 5, 0, 0]

tic = time.process_time()
dot = 0
for i in range(len(x1)):
    dot += x1[i] *x2[i]
toc = time.process_time()
print("dot = " + str(dot) + "\n ----- Computation time = " + str(1000*(toc - tic)) + "ms")

tic = time.process_time()
outer = np.zeros((len(x1), len(x2)))
for i in range(len(x1)):
    for j in range(len(x2)):
        outer[i,j] = x1[i]*x2[j]
toc = time.process_time()
print ("outer = " + str(outer) + "\n ----- Computation time = " + str(1000*(toc - tic)) + "ms")

tic = time.process_time()
mul = np.zeros(len(x1))
for i in range(len(x1)):
    mul[i] = x1[i]*x2[i]
toc = time.process_time()
print ("elementwise multiplication = " + str(mul) + "\n ----- Computation time = " + str(1000*(toc - tic)) + "ms")

W = np.random.rand(3,len(x1)) # Random 3*len(x1) numpy array
tic = time.process_time()
gdot = np.zeros(W.shape[0])
for i in range(W.shape[0]):
    for j in range(len(x1)):
        gdot[i] += W[i,j]*x1[j]
toc = time.process_time()
print ("gdot = " + str(gdot) + "\n ----- Computation time = " + str(1000*(toc - tic)) + "ms")

def L1(yhat, y):
    loss = sum(abs(y-yhat))
    return loss

yhat = np.array([0.9, 0.2,0.1,0.4,0.9])
y = np.array([1,0,0,1,1])
print("l1 = " + str(L1(yhat,y)))

def L2(yhat, y):
    loss = np.dot(y-yhat, y-yhat)
    return loss

yhat = np.array([.9,.2,.1,.4,.9])
y = np.array([1,0,0,1,1])
print("l2 = " + str(L2(yhat,y)))

import matplotlib.pyplot as plt
import h5py
import scipy
from PIL import Image
from scipy import ndimage
import dp_ai.seaborn.lr_utils
from dp_ai.seaborn.lr_utils import load_dataset

train_set_x_orig, train_set_y, test_set_x_orig, test_set_y, classes = load_dataset()
index = 25
plt.imshow(train_set_x_orig[index])
print ("y = " + str(train_set_y[:, index]) + ", it's a '" + classes[np.squeeze(train_set_y[:, index])].decode("utf-8") + "'picture.")

train_set_x_flatten = train_set_x_orig.reshape(train_set_x_orig.shape[0],-1).T
test_set_x_flatten = test_set_x_orig.reshape(test_set_x_orig.shape[0],-1).T

def initialize_with_zeros(dim):
    w = np.zeros((dim,1))
    b = 0
    assert (w.shape == (dim,1))
    assert (isinstance(b, float) or isinstance(b,int))
    return w, b

def propagate(w,b,X,Y):

    m = X.shape[1]
    A = sigmoid(np.dot(w.T, X) + b)
    cost = -(np.dot(Y, np.log(A.T)) + np.dot(np.log(1-A), (1-Y).T))/m
    dw = np.dot(X, (A-Y).T)/m
    db = np.sum(A-Y)/m

    assert(dw.shape == w.shape)
    assert(db.dtype == float)
    cost = np.squeeze(cost)
    assert(cost.shape == ())

    grads = {"dw": dw,
             "db": db}
    return grads, cost

def optimize(w,b,X,Y,num_iterations, learning_rate, print_cost = False):
    costs = []
    for i in range(num_iterations):
        grads, cost = propagate(w,b,X,Y)

    dw = grads["dw"]
    db = grads["db"]

    w = w - learning_rate*dw
    b = b - learning_rate * db

    if i % 100 == 0:
        costs.append(cost)

    if print_cost and i % 100 == 0:
        print("Cost after iteration %i: %f" % (i, cost))

    params = {"w": w,
              "b": b}
    grads = {"dw": dw,
             "db": db}
    return params, grads, costs

w, b, X, Y = np.array([[1.],[2.]]), 2., np.array([[1.,2.,-1.],[3.,4.,-3.2]]), np.array([[1,0,1]])
params, grads, costs = optimize(w, b, X, Y, num_iterations= 100,learning_rate = 0.009, print_cost = False)
print ("w = " + str(params["w"]))
print ("b = " + str(params["b"]))
print ("dw = " + str(grads["dw"]))
print ("db = " + str(grads["db"]))

def predict(w,b,X):
    m = X.shape[1]
    Y_prediction = np.zeros((1,m))
    w = w.reshape(X.shape[0],1)

    A = sigmoid(np.dot(w.T, X), b)
    for i in range(A.shape[1]):
        if A[0][j] <= 0.5:
            A[0][j] = 0
        else:
            A[0][j] = 1
    Y_prediction = A
    assert(Y_prediction.shape == (1,m))
    return Y_prediction

def model(X_train, Y_train, X_test, Y_test, num_iterations = 2000, learning_rate = 0.5,print_cost = False):
    w, b = initialize_with_zeros(X_train.shape[0])

    parameters, grads, costs = optimize(w,b,X_train, Y_train, num_iterations, learning_rate, print_cost)
    w = parameters["w"]
    b = parameters["b"]

    Y_prediction_test = predict(w,b,X_test)
    Y_prediction_train = predict(w, b, X_train)

    print("train accuracy: {} %".format(100 - np.mean(np.abs(Y_prediction_train - Y_train)) * 100))
    print("test accuracy: {} %".format(100 - np.mean(np.abs(Y_prediction_test - Y_test)) * 100))

    d = {"costs": costs,
         "Y_prediction_test": Y_prediction_test,
         "Y_prediction_train": Y_prediction_train,
         "w": w,
         "b": b,
         "learning_rate": learning_rate,
         "num_iterations": num_iterations}
    return d

train_set_x_flatten = train_set_x_orig.reshape(train_set_x_orig.shape[0],-1).T
test_set_x_flatten = test_set_x_orig.reshape(test_set_x_orig.shape[0],-1).T

# d = model(train_set_x, train_set_y, test_set_x, test_set_y,num_iterations = 2000, learning_rate = 0.005, print_cost = True)


import sys
input = sys.stdin.readline
from collections import deque
dx = [-1, 1, 0 ,0]
dy = [0, 0, -1, 1]

def bfs(i, j):
    q = deque()
    q.append((i, j))
    cnt = 0
    while q:
        x, y = q.popleft()
        visited[x][y] = 1
        cnt += 1
        for d in range(4):
            nx = x + dx[d]
            ny = y + dy[d]
            if 0 <= nx < N and 0 <= ny < M and not visited[nx][ny] and maps[nx][ny]:
                q.append((nx, ny))
                visited[nx][ny] = 1
    return cnt

N, M, K = map(int, input().split())
maps = [[0] * M for _ in range(N)]
visited = [[0] * M for _ in range(N)]
ans = 0
for _ in range(K):
    r, c = map(int, input().split())
    maps[r-1][c-1] = 1

for i in range(N):
    for j in range(M):
        if not visited[i][j] and maps[i][j]:
            chk = bfs(i, j)
            if ans < chk:
                ans = chk
print(ans)

백준/Silver/1743. 음식물 피하기/음식물 피하기.py


from PyQt5.QtWidgets import QTableWidget, QTableWidgetItem

class CustomFieldTable(QTableWidget):
    def addCustomField(self, key=None, value=None):
        rc=self.rowCount()
        self.insertRow(rc)

        if key is not None:
            twi = QTableWidgetItem
            if value is None:
                value = ''
            self.setItem(rc, 0, twi(key))
            self.setItem(rc, 1, twi(value))

    def removeCustomField(self):
        row = self.currentRow()

        if row >= 0:
            self.removeRow(row)

    def getCustomFields(self):
        rc = self.rowCount()
        for i in range(0,rc):
            try:
                field = self.takeItem(i, 0).text()
            except:
                continue
            try:
                value = self.takeItem(i, 1).text()
            except:
                value = None
            yield (field, value)

    def clear(self):
        rc = self.rowCount()
        for i in range(0,rc):
            self.removeRow(i)


from celery import Celery, Task, current_app

import base64
import io
from PIL import Image, ImageDraw, ImageFont
import textwrap
import json
import requests
from celery.contrib import rdb

app = Celery('tasks', CELERY_RESULT_BACKEND="rpc://", broker='amqp://')

addr = 'http://127.0.0.1:5000'
result_url = addr + '/result'

# prepare headers for http request
content_type = 'image/jpg'
headers = {'content-type': content_type}

def check_text_size_fit(text_desc, text_size, font_family, rect_width, rect_height):
    """ Check if the text will fit inside the rectangle """
    #Create text font
    text_font = ImageFont.truetype(font_family, size=text_size)

    #Wrap text based on width of the rectangle
    avg_char_width = sum(text_font.getsize(char)[0] for char in text_desc) / len(text_desc)
    max_char_count = int((rect_width) / avg_char_width)

    if max_char_count < 1:
        return 0

    wrapped_text = textwrap.fill(text=text_desc, width=max_char_count)
    text_lines_num = wrapped_text.count('\n')+1

    #Text fits inside rectangle
    if( text_size*text_lines_num <= rect_height):
        return 1

def image_rectangle_text(img, text, coords, position, text_color, text_size, font_family):
    draw = ImageDraw.Draw(img)

    #Grab text and rectangle info
    text_description = text
    rectangle = coords

    #Get rectangle width and height
    rect_width = rectangle[2]-rectangle[0]
    rect_height = rectangle[3]-rectangle[1]

    #Check if text fits rectangle, lower text size if it doesn't
    text_size_fits = False
    while(not(text_size_fits)):
        try:
            result = check_text_size_fit(text_description,text_size,font_family,rect_width,rect_height)
        except Exception as e:
            print(str(e))
            result = 0
        if( result != 1):
            text_size -= 1
        else:
            text_size_fits = True

    #Create text font
    text_font = ImageFont.truetype(font_family, size=text_size)

    #Split text if there are newlines
    text_description = text_description.split('\n')

    line_pos = 0    #Keep track of line position

    #Go through each text segment
    for text_desc in text_description:
        #Wrap text based on width of the rectangle
        print(text_font.getsize(text_desc[0]))
        avg_char_width = text_font.getsize(text_desc)[0] / len(text_desc)
        max_char_count = int((rect_width) / avg_char_width)
        wrapped_text = textwrap.fill(text=text_desc, width=max_char_count)
        text_lines_num = wrapped_text.count('\n')+1

        #Get the width of text box
        text_width = text_font.getsize(wrapped_text.split('\n')[0])[0]

        #Draw Rectangle for Debugging Only
        # draw.rectangle([(rectangle[0],rectangle[1]),(rectangle[2],rectangle[3])],outline="red") # Uncomment to see rectangle

        #Draw the text based on position
        if position == "top-left":
            draw.text((rectangle[0], rectangle[1]+(text_size*line_pos)), wrapped_text, fill =text_color, font=text_font)
        if position == "top-center":
            draw.text((rectangle[0]+int(rect_width/2)-int(text_width/2), rectangle[1]+(text_size*line_pos)), wrapped_text, fill =text_color, font=text_font)
        if position == "top-right":
            draw.text((rectangle[2]-text_width, rectangle[1]+(text_size*line_pos)), wrapped_text, fill =text_color, font=text_font)

        if position == "center-left":
            draw.text((rectangle[0], rectangle[1]+int(rect_height/2)-int(text_size*text_lines_num/2)+(text_size*line_pos)), wrapped_text, fill =text_color, font=text_font)
        if position == "center":
            draw.text((rectangle[0]+int(rect_width/2)-int(text_width/2), rectangle[1]+int(rect_height/2)-int(text_size*text_lines_num/2)+(text_size*line_pos)), wrapped_text, fill=text_color, font=text_font)
        if position == "center-right":
            draw.text((rectangle[2]-text_width, rectangle[1]+int(rect_height/2)-int(text_size*text_lines_num/2)+(text_size*line_pos)), wrapped_text, fill =text_color, font=text_font)

        if position == "below-left":
            draw.text(((rectangle[0], rectangle[3]-(text_size*text_lines_num)+(text_size*line_pos))), wrapped_text, fill =text_color, font=text_font)
        if position == "below-center":
            draw.text((rectangle[0]+int(rect_width/2)-int(text_width/2), rectangle[3]-(text_size*text_lines_num)+(text_size*line_pos)), wrapped_text, fill =text_color, font=text_font)
        if position == "below-right":
            draw.text((rectangle[2]-text_width, rectangle[3]-(text_size*text_lines_num)+(text_size*line_pos)), wrapped_text, fill =text_color, font=text_font)

        line_pos+=text_lines_num

    return img

@app.task
def process_image_text(image_string, text_and_boxes):
    processed_image_str = ""
    try:
        image = io.BytesIO(base64.b64decode(image_string.encode('utf-8')))
        img = Image.open(image)

        for textbox in text_and_boxes:
            text = textbox['text']
            coords = textbox['coords']
            position = textbox['position']
            color = tuple(textbox['color'])
            text_size = textbox['text_size']
            font = textbox['font']
            img = image_rectangle_text(img, text, coords, position, tuple(color), text_size, font)

        byte_io = io.BytesIO()
        img.save(byte_io,'PNG')
        processed_image_str = base64.b64encode(byte_io.getvalue()).decode('utf-8')

    except Exception as e:
        print(str(e))

    # send http request with image and receive response
    requests.post(result_url, data=processed_image_str, headers=headers)
    return 1


import os
import io
import sys
from tempfile import TemporaryFile

STDOUT = 1
STDERR = 2
SYS_FDS = {
    STDOUT: 'stdout',
    STDERR: 'stderr',
}

class CallbackFile(io.TextIOWrapper):
    def __init__(self, callback: callable = None):
        super().__init__(
            TemporaryFile(buffering=0),
            encoding='utf-8',
            errors='replace',
            write_through=True,
            newline='',
        )
        self.callback = callback

    def write(self, data) -> None:
        super().write(data)
        if '\n' in data:
            self.flush()

    def getvalue(self) -> str:
        self.buffer.seek(0)
        res = self.buffer.read()
        self.buffer.seek(0)
        self.buffer.truncate()
        return res.decode('utf-8')

    def flush(self) -> None:
        if self.callback is not None:
            text = self.getvalue()
            if len(text) > 0:
                self.callback(text)
        else:
            super().flush()

class FDCapture:
    def __init__(self, targetfd: int, callback: callable = None) -> None:
        self.targetfd = targetfd
        self.targetfd_save = os.dup(targetfd)
        self.callback = callback
        self.tmpfile = CallbackFile(callback)
        self._capturing = False

    def start(self) -> None:
        """Start capturing on targetfd using memorized tmpfile."""
        assert not self._capturing

        # redirect the target fd to our temporary file
        os.dup2(self.tmpfile.fileno(), self.targetfd)

        if self.targetfd in SYS_FDS:
            # for system file descriptors (stdout, stderr) we also need to replace the sys.stdout/sys.stderr streams.
            # this allows us to capture output from the local python process
            self.sysfile = getattr(sys, SYS_FDS[self.targetfd])
            setattr(sys, SYS_FDS[self.targetfd], self.tmpfile)

        self._capturing = True

    def stop(self) -> None:
        """Stop capturing, restore streams & return original capture file"""
        assert self._capturing

        # disable output redirection
        os.dup2(self.targetfd_save, self.targetfd)

        # if we have a callback, flush any remaining data
        if self.callback:
            remainder = self.getvalue()
            if len(remainder) > 0:
                self.callback(remainder)

        # close temporary file descriptor
        os.close(self.targetfd_save)

        if self.targetfd in SYS_FDS:
            # restore the original sys stream
            setattr(sys, SYS_FDS[self.targetfd], self.sysfile)

        self._capturing = False

    def writeorg(self, data) -> None:
        """Write to original file descriptor."""
        if self._capturing:
            os.write(self.targetfd_save, data.encode('utf-8'))
        else:
            os.write(self.targetfd, data.encode('utf-8'))

    def getvalue(self) -> str:
        return self.tmpfile.getvalue()

    def __del__(self) -> None:
        self.tmpfile.close()

class FileCapturing(object):
    def __init__(
        self,
        on_stdout: callable = None,
        on_stderr: callable = None,
    ):
        self.stdout = FDCapture(STDOUT, on_stdout)
        self.stderr = FDCapture(STDERR, on_stderr)

    def __enter__(self) -> None:
        self.stdout.start()
        self.stderr.start()
        return self

    def __exit__(self, *args) -> None:
        self.stdout.stop()
        self.stderr.stop()

    def __del__(self) -> None:
        del self.stdout
        del self.stderr


from __future__ import annotations

import json
import os
import re
import warnings
from functools import wraps

from dynaconf.utils import extract_json_objects
from dynaconf.utils import isnamedtupleinstance
from dynaconf.utils import multi_replace
from dynaconf.utils import recursively_evaluate_lazy_format
from dynaconf.utils.boxing import DynaBox
from dynaconf.utils.functional import empty
from dynaconf.vendor import toml
from dynaconf.vendor import tomllib

try:
    from jinja2 import Environment

    jinja_env = Environment()
    for p_method in ("abspath", "realpath", "relpath", "dirname", "basename"):
        jinja_env.filters[p_method] = getattr(os.path, p_method)
except ImportError:  # pragma: no cover
    jinja_env = None

true_values = ("t", "true", "enabled", "1", "on", "yes", "True")
false_values = ("f", "false", "disabled", "0", "off", "no", "False", "")

KV_PATTERN = re.compile(r"([a-zA-Z0-9 ]*=[a-zA-Z0-9\- :]*)")
"""matches `a=b, c=d, e=f` used on `VALUE='@merge foo=bar'` variables."""

class DynaconfFormatError(Exception):
    """Error to raise when formatting a lazy variable fails"""

class DynaconfParseError(Exception):
    """Error to raise when parsing @casts"""

class MetaValue:
    """A Marker to trigger specific actions on `set` and `object_merge`"""

    _meta_value = True

    def __init__(self, value, box_settings):
        self.box_settings = box_settings
        self.value = parse_conf_data(
            value, tomlfy=True, box_settings=box_settings
        )

    def __repr__(self):
        return f"{self.__class__.__name__}({self.value}) on {id(self)}"

    def unwrap(self):
        return self.value

class Reset(MetaValue):
    """Triggers an existing key to be reset to its value
    NOTE: DEPRECATED on v3.0.0
    """

    _dynaconf_reset = True

    def __init__(self, value, box_settings):
        self.box_settings = box_settings
        self.value = parse_conf_data(
            value, tomlfy=True, box_settings=self.box_settings
        )
        warnings.warn(f"{self.value} does not need `@reset` anymore.")

class Del(MetaValue):
    """Triggers an existing key to be deleted"""

    _dynaconf_del = True

    def unwrap(self):
        raise ValueError("Del object has no value")

class Merge(MetaValue):
    """Triggers an existing key to be merged"""

    _dynaconf_merge = True

    def __init__(self, value, box_settings, unique=False):
        if unique:
            self._dynaconf_merge_unique = True

        self.box_settings = box_settings

        self.value = parse_conf_data(
            value, tomlfy=True, box_settings=box_settings
        )

        if isinstance(self.value, (int, float, bool)):
            # @merge 1, @merge 1.1, @merge False
            self.value = [self.value]
        elif isinstance(self.value, str):
            # @merge {"valid": "json"}
            json_object = list(
                extract_json_objects(
                    multi_replace(
                        self.value,
                        {
                            ": True": ": true",
                            ":True": ": true",
                            ": False": ": false",
                            ":False": ": false",
                            ": None": ": null",
                            ":None": ": null",
                        },
                    )
                )
            )
            if len(json_object) == 1:
                self.value = json_object[0]
            else:
                matches = KV_PATTERN.findall(self.value)
                # a=b, c=d
                if matches:
                    self.value = {
                        k.strip(): parse_conf_data(
                            v, tomlfy=True, box_settings=box_settings
                        )
                        for k, v in (
                            match.strip().split("=") for match in matches
                        )
                    }
                elif "," in self.value:
                    # @merge foo,bar
                    self.value = self.value.split(",")
                else:
                    # @merge foo
                    self.value = [self.value]

        self.unique = unique

class BaseFormatter:
    def __init__(self, function, token):
        self.function = function
        self.token = token

    def __call__(self, value, **context):
        try:
            return self.function(value, **context)
        except (KeyError, AttributeError) as exc:
            # A template like `{this.KEY}` failed with AttributeError
            # Or KeyError in the case of `{env[KEY]}`
            raise DynaconfFormatError(
                f"Dynaconf can't interpolate variable because {exc}"
            ) from exc

    def __str__(self):
        return str(self.token)

def _jinja_formatter(value, **context):
    if jinja_env is None:  # pragma: no cover
        raise ImportError(
            "jinja2 must be installed to enable '@jinja' settings in dynaconf"
        )
    return jinja_env.from_string(value).render(**context)

class Formatters:
    """Dynaconf builtin formatters"""

    python_formatter = BaseFormatter(str.format, "format")
    jinja_formatter = BaseFormatter(_jinja_formatter, "jinja")

class Lazy:
    """Holds data to format lazily."""

    _dynaconf_lazy_format = True

    def __init__(
        self, value=empty, formatter=Formatters.python_formatter, casting=None
    ):
        self.value = value
        self.formatter = formatter
        self.casting = casting

    @property
    def context(self):
        """Builds a context for formatting."""
        return {"env": os.environ, "this": self.settings}

    def __call__(self, settings, validator_object=None):
        """LazyValue triggers format lazily."""
        self.settings = settings
        self.context["_validator_object"] = validator_object
        result = self.formatter(self.value, **self.context)
        if self.casting is not None:
            result = self.casting(result)
        return result

    def __str__(self):
        """Gives string representation for the object."""
        return str(self.value)

    def __repr__(self):
        """Give the quoted str representation"""
        return f"'@{self.formatter} {self.value}'"

    def _dynaconf_encode(self):
        """Encodes this object values to be serializable to json"""
        return f"@{self.formatter} {self.value}"

    def set_casting(self, casting):
        """Set the casting and return the instance."""
        self.casting = casting
        return self

def try_to_encode(value, callback=str):
    """Tries to encode a value by verifying existence of `_dynaconf_encode`"""
    try:
        return value._dynaconf_encode()
    except (AttributeError, TypeError):
        return callback(value)

def evaluate_lazy_format(f):
    """Marks a method on Settings instance to
    lazily evaluate LazyFormat objects upon access."""

    @wraps(f)
    def evaluate(settings, *args, **kwargs):
        value = f(settings, *args, **kwargs)
        return recursively_evaluate_lazy_format(value, settings)

    return evaluate

converters = {
    "@str": lambda value: value.set_casting(str)
    if isinstance(value, Lazy)
    else str(value),
    "@int": lambda value: value.set_casting(int)
    if isinstance(value, Lazy)
    else int(value),
    "@float": lambda value: value.set_casting(float)
    if isinstance(value, Lazy)
    else float(value),
    "@bool": lambda value: value.set_casting(
        lambda x: str(x).lower() in true_values
    )
    if isinstance(value, Lazy)
    else str(value).lower() in true_values,
    "@json": lambda value: value.set_casting(
        lambda x: json.loads(x.replace("'", '"'))
    )
    if isinstance(value, Lazy)
    else json.loads(value),
    "@format": lambda value: Lazy(value),
    "@jinja": lambda value: Lazy(value, formatter=Formatters.jinja_formatter),
    # Meta Values to trigger pre assignment actions
    "@reset": Reset,  # @reset is DEPRECATED on v3.0.0
    "@del": Del,
    "@merge": Merge,
    "@merge_unique": lambda value, box_settings: Merge(
        value, box_settings, unique=True
    ),
    # Special markers to be used as placeholders e.g: in prefilled forms
    # will always return None when evaluated
    "@note": lambda value: None,
    "@comment": lambda value: None,
    "@null": lambda value: None,
    "@none": lambda value: None,
    "@empty": lambda value: empty,
}

def apply_converter(converter_key, value, box_settings):
    """
    Get converter and apply it to @value.

    Lazy converters will return Lazy objects for later evaluation.
    """
    converter = converters[converter_key]
    try:
        converted_value = converter(value, box_settings=box_settings)
    except TypeError:
        converted_value = converter(value)
    return converted_value

def add_converter(converter_key, func):
    """Adds a new converter to the converters dict"""
    if not converter_key.startswith("@"):
        converter_key = f"@{converter_key}"

    converters[converter_key] = wraps(func)(
        lambda value: value.set_casting(func)
        if isinstance(value, Lazy)
        else Lazy(
            value,
            casting=func,
            formatter=BaseFormatter(lambda x, **_: x, converter_key),
        )
    )

def parse_with_toml(data):
    """Uses TOML syntax to parse data"""
    try:  # try tomllib first
        try:
            return tomllib.loads(f"key={data}")["key"]
        except (tomllib.TOMLDecodeError, KeyError):
            return data
    except UnicodeDecodeError:  # pragma: no cover
        # fallback to toml (TBR in 4.0.0)
        try:
            return toml.loads(f"key={data}")["key"]
        except (toml.TomlDecodeError, KeyError):
            return data
        warnings.warn(
            "TOML files should have only UTF-8 encoded characters. "
            "starting on 4.0.0 dynaconf will stop allowing invalid chars.",
            DeprecationWarning,
        )

def _parse_conf_data(data, tomlfy=False, box_settings=None):
    """
    @int @bool @float @json (for lists and dicts)
    strings does not need converters

    export DYNACONF_DEFAULT_THEME='material'
    export DYNACONF_DEBUG='@bool True'
    export DYNACONF_DEBUG_TOOLBAR_ENABLED='@bool False'
    export DYNACONF_PAGINATION_PER_PAGE='@int 20'
    export DYNACONF_MONGODB_SETTINGS='@json {"DB": "quokka_db"}'
    export DYNACONF_ALLOWED_EXTENSIONS='@json ["jpg", "png"]'
    """
    # not enforced to not break backwards compatibility with custom loaders
    box_settings = box_settings or {}

    castenabled = box_settings.get("AUTO_CAST_FOR_DYNACONF", empty)
    if castenabled is empty:
        castenabled = (
            os.environ.get("AUTO_CAST_FOR_DYNACONF", "true").lower()
            not in false_values
        )

    if (
        castenabled
        and data
        and isinstance(data, str)
        and data.startswith(tuple(converters.keys()))
    ):
        # Check combination token is used
        comb_token = re.match(
            f"^({'|'.join(converters.keys())}) @(jinja|format)",
            data,
        )
        if comb_token:
            tokens = comb_token.group(0)
            converter_key_list = tokens.split(" ")
            value = data.replace(tokens, "").strip()
        else:
            parts = data.partition(" ")
            converter_key_list = [parts[0]]
            value = parts[-1]

        # Parse the converters iteratively
        for converter_key in converter_key_list[::-1]:
            value = apply_converter(converter_key, value, box_settings)
    else:
        value = parse_with_toml(data) if tomlfy else data

    if isinstance(value, dict):
        value = DynaBox(value, box_settings=box_settings)

    return value

def parse_conf_data(data, tomlfy=False, box_settings=None):
    """
    Apply parsing tokens recursively and return transformed data.

    Strings with lazy parser (e.g, @format) will become Lazy objects.
    """

    # fix for https://github.com/dynaconf/dynaconf/issues/595
    if isnamedtupleinstance(data):
        return data

    # not enforced to not break backwards compatibility with custom loaders
    box_settings = box_settings or {}

    if isinstance(data, (tuple, list)):
        # recursively parse each sequence item
        return [
            parse_conf_data(item, tomlfy=tomlfy, box_settings=box_settings)
            for item in data
        ]

    if isinstance(data, DynaBox):
        # recursively parse inner dict items
        _parsed = DynaBox({}, box_settings=box_settings)
        for k, v in data._safe_items():
            _parsed[k] = parse_conf_data(
                v, tomlfy=tomlfy, box_settings=box_settings
            )
        return _parsed

    if isinstance(data, dict):
        # recursively parse inner dict items
        _parsed = {}
        for k, v in data.items():
            _parsed[k] = parse_conf_data(
                v, tomlfy=tomlfy, box_settings=box_settings
            )
        return _parsed

    # return parsed string value
    return _parse_conf_data(data, tomlfy=tomlfy, box_settings=box_settings)

def unparse_conf_data(value):
    if isinstance(value, bool):
        return f"@bool {value}"

    if isinstance(value, int):
        return f"@int {value}"

    if isinstance(value, float):
        return f"@float {value}"

    if isinstance(value, (list, dict)):
        return f"@json {json.dumps(value)}"

    if isinstance(value, Lazy):
        return try_to_encode(value)

    if value is None:
        return "@none "

    return value

def boolean_fix(value: str | None):
    """Gets a value like `True/False` and turns to `true/false`
    This function exists because of issue #976
    Toml parser casts booleans from true/false lower case
    however envvars are usually exportes as True/False capitalized
    by mistake, this helper fixes it for envvars only.

    Assume envvars are always str.
    """
    if value and value.strip() in ("True", "False"):
        return value.lower()
    return value


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sat Sep 12 11:06:59 2020

@author: arjun
"""

import logging
# Imports the Google Cloud client library

from flask import Flask, request,render_template,jsonify
from models import db, Ballot,Candidate, User
import json
import traceback
import requests

blockchain_service_url = "http://0.0.0.0:5006/blockchain"
notification_service_url = "http://0.0.0.0:5005/notification"
ballot_service_url = "http://192.168.0.102:5003"

app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = "mysql+pymysql://root:root@127.0.0.1:3306/evdb"

db.init_app(app)

with app.app_context():
    db.create_all()

@app.route('/ballot', methods=["POST"])
def vote():
    '''
    create a new poll.
    callers: all users
    '''
    data = request.form.to_dict()
    ballot = Ballot()
    try:
        payload = {'candidate_id': data["candidate_id"]}
        response = requests.request("POST", blockchain_service_url+"/vote", data = payload)
        data["transaction_id"] = json.loads(response.text)["receipt"]
        ballot.deserialize(json=data)
        ballot.save()
        response_object = ballot.serialize()

        try:
            payload = {
                    'number': User.query.filter(
                            User.id==response_object["user_id"]
                            ).first().phone,
                    'link': ballot_service_url+"/"+str(response_object["id"])
                    }

            requests.request("POST", notification_service_url, data = payload)
        except:
            traceback.print_exc()
            pass

        return json.dumps(response_object),200
    except Exception as e:
        traceback.print_exc()
        return str(e),500

@app.route('/ballot/<int:id>', methods=["GET"])
def get_ballot_object(id):
    '''
    get ballot object by id
    callers: system
    '''
    ballot = Ballot.query.filter(Ballot.id == id).first()
    if ballot:
        ballot = ballot.serialize()
        return json.dumps(ballot)
    else:
        return "",404

@app.route('/<int:id>', methods=["GET"])
def short_get_ballot_object(id):
    '''
    get ballot object by id
    callers: system
    '''
    ballot = Ballot.query.filter(Ballot.id == id).first()
    if ballot:
        ballot = ballot.serialize()

        response = requests.request("GET", blockchain_service_url+"/results");

        if response:
            results = json.loads(response.text)["result"]

        return render_template('layouts/index.html',
                               voter_data=ballot,
                               results=results
                               )
    else:
        return "not found",404

@app.route('/candidates', methods=["GET"])
def list_candidates():
    '''
    list candidates
    callers: all users
    '''
    candidates = Candidate.query.all()

    if candidates:
        candidate_list = [candidate.serialize() for candidate in candidates]

        return json.dumps(candidate_list)
    else:
        return "",404

@app.route('/')
def hello():
    return 'Ballot Service is active'

@app.errorhandler(500)
def server_error(e):
    # Log the error and stacktrace.
    logging.exception('An error occurred during a request.')
    return 'An internal error occurred.', 500
# [END app]

if __name__ =="__main__":
    app.run(debug=True,host="0.0.0.0",port=5003)


# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.

import os
import platform
import subprocess
from distutils.version import StrictVersion

from mozboot.util import get_tools_dir
from mozfile import which
from six import PY3

NODE_MIN_VERSION = StrictVersion("12.22.12")
NPM_MIN_VERSION = StrictVersion("6.14.16")

def find_node_paths():
    """Determines the possible paths for node executables.

    Returns a list of paths, which includes the build state directory.
    """
    mozbuild_tools_dir = get_tools_dir()

    if platform.system() == "Windows":
        mozbuild_node_path = os.path.join(mozbuild_tools_dir, "node")
    else:
        mozbuild_node_path = os.path.join(mozbuild_tools_dir, "node", "bin")

    # We still fallback to the PATH, since on OSes that don't have toolchain
    # artifacts available to download, Node may be coming from $PATH.
    paths = [mozbuild_node_path] + os.environ.get("PATH").split(os.pathsep)

    if platform.system() == "Windows":
        paths += [
            "%s\\nodejs" % os.environ.get("SystemDrive"),
            os.path.join(os.environ.get("ProgramFiles"), "nodejs"),
            os.path.join(os.environ.get("PROGRAMW6432"), "nodejs"),
            os.path.join(os.environ.get("PROGRAMFILES"), "nodejs"),
        ]

    return paths

def check_executable_version(exe, wrap_call_with_node=False):
    """Determine the version of a Node executable by invoking it.

    May raise ``subprocess.CalledProcessError`` or ``ValueError`` on failure.
    """
    out = None
    # npm may be a script (Except on Windows), so we must call it with node.
    if wrap_call_with_node and platform.system() != "Windows":
        binary, _ = find_node_executable()
        if binary:
            out = (
                subprocess.check_output(
                    [binary, exe, "--version"], universal_newlines=PY3
                )
                .lstrip("v")
                .rstrip()
            )

    # If we can't find node, or we don't need to wrap it, fallback to calling
    # direct.
    if not out:
        out = (
            subprocess.check_output([exe, "--version"], universal_newlines=PY3)
            .lstrip("v")
            .rstrip()
        )
    return StrictVersion(out)

def find_node_executable(
    nodejs_exe=os.environ.get("NODEJS"), min_version=NODE_MIN_VERSION
):
    """Find a Node executable from the mozbuild directory.

    Returns a tuple containing the the path to an executable binary and a
    version tuple. Both tuple entries will be None if a Node executable
    could not be resolved.
    """
    if nodejs_exe:
        try:
            version = check_executable_version(nodejs_exe)
        except (subprocess.CalledProcessError, ValueError):
            return None, None

        if version >= min_version:
            return nodejs_exe, version.version

        return None, None

    # "nodejs" is first in the tuple on the assumption that it's only likely to
    # exist on systems (probably linux distros) where there is a program in the path
    # called "node" that does something else.
    return find_executable("node", min_version)

def find_npm_executable(min_version=NPM_MIN_VERSION):
    """Find a Node executable from the mozbuild directory.

    Returns a tuple containing the the path to an executable binary and a
    version tuple. Both tuple entries will be None if a Node executable
    could not be resolved.
    """
    return find_executable("npm", min_version, True)

def find_executable(name, min_version, use_node_for_version_check=False):
    paths = find_node_paths()
    exe = which(name, path=paths)

    if not exe:
        return None, None

    # Verify we can invoke the executable and its version is acceptable.
    try:
        version = check_executable_version(exe, use_node_for_version_check)
    except (subprocess.CalledProcessError, ValueError):
        return None, None

    if version < min_version:
        return None, None

    return exe, version.version


"""
"""

from collections import OrderedDict as OD
import re

import sympy
import numpy as np

from SloppyCell.ReactionNetworks.Reactions import Reaction

from util2 import butil
reload(butil)

def get_substrates(stoich, multi=False):
    """
    Input:
        multi:
    """
    for spid, stoichcoef in stoich.items():
        # could be ...
        stoichcoef = int(float(stoichcoef))

    subs = [[spid]*abs(int(float(stoichcoef))) for (spid, stoichcoef)
            in stoich.items() if stoichcoef<0]
    subs = butil.flatten(subs)
    if not multi:
        subs = list(set(subs))
    subs = sorted(subs)  # make spids appear in order
    return subs

def get_products(stoich, multi=False):
    pros = [[spid]*abs(stoichcoef) for (spid, stoichcoef)
            in stoich.items() if stoichcoef>0]
    pros = butil.flatten(pros)
    if not multi:
        pros = list(set(pros))
    pros = sorted(pros)  # make spids appear in order
    return pros

def get_reactants(stoich, multi=True):
    """
    Sometimes a stoichiometry has species that are not reactants, whose
    stoichiometry coefficients are zero.
    """
    return get_substrates(stoich, multi) + get_products(stoich, multi)

def get_merged_stoich(*stoichs):
    """
    """
    spids = sorted(set(butil.flatten([stoich.keys() for stoich in stoichs])))
    stoich_m = type(stoichs[0]).fromkeys(spids, 0)
    for stoich in stoichs:
        for spid, stoichcoef in stoich.items():
            stoich_m[spid] += stoichcoef
    for spid, stoichcoef in stoich_m.items():
        if stoichcoef == 0:
            del stoich_m[spid]
    return stoich_m

def reverse_stoich(stoich):
    """

    """
    stoich_b = type(stoich).fromkeys(stoich.keys(), 0)  # b for backward
    for spid, stoichcoef in stoich.items():
        stoich_b[spid] = -1 * stoichcoef
    return stoich_b

def get_all_states(spids):
    """
    """
    states = butil.get_powerset(spids)
    states_sort = sorted([tuple(sorted(s)) for s in states])
    return states_sort

def stoich2eqn(stoich):
    pass

def eqn2stoich(eqn):
    """
    Convert reaction equation (a string) to stoichiometry (a dictionary).
    """
    def unpack(s):
        # an example of s: ' 2 ATP '
        l = filter(None, s.split(' '))
        if len(l) == 1:
            stoichcoef, spid = '1', l[0]
        if len(l) == 2:
            stoichcoef, spid = l
        return spid, stoichcoef

    # re: '<?': 0 or 1 '<'; '[-|=]': '-' or '='
    subs, pros = re.split('<?[-|=]>', eqn)
    stoich = OD([])

    for sub in subs.split('+'):
        subid, stoichcoef = unpack(sub)
        stoich[subid] = '-' + stoichcoef
    for pro in pros.split('+'):
        proid, stoichcoef = unpack(pro)
        stoich[proid] = stoichcoef

    return stoich

def get_ratelaw_MM(rxnid, stoich, reversible=True,
                   Keq=None, KIcs=None, KIus=None, KAs=None,
                   states_allowed=None, states_add=None, one_step=False):
    """
    Construct rate law expressions for Michaelis-Menten kinetics
    with quasi-equilibrium assumption.

    Input:
        stoich: a dict mapping from species ids to stoich coefs,
                e.g., {'S':-1, 'P':1, 'I':0}
        Keq: a number, e.g., 1e2
        KIcs: a dict mapping from ids of competitive inhibitors to
              inhibition constants,
              e.g., {'I1':2.5e-2, 'I2':1e-1}
        KIus: a dict mapping from ids of uncompetitive inhibitors to
              inhibition constants,
              e.g., {'I1':2.5e-2, 'I2':1e-1}
        states_allowed: a list of tuples, orders not matter
        one_step: one step binding used by Alex Shestov; if True, assume...

    Output:

    """
    # Abbreviations:
    # sub: substrate
    # pro: product
    # s: state
    # f: forward
    # b: backward

    subs = get_substrates(stoich, multi=True)
    pros = get_products(stoich, multi=True)
    #state_allsubs = tuple(subs)
    #state_allpros = tuple(pros)
    KMs_sub = ['KM_%s_%s'%(rxnid, sub) for sub in subs]
    KMs_pro = ['KM_%s_%s'%(rxnid, pro) for pro in pros]
    spid2KM = dict(zip(subs+pros, KMs_sub+KMs_pro))

    # make a dictionary, state2term, that maps from binding state
    # of the enzyme to the corresponding term
    states = [()]
    if one_step:
        states.append(tuple(subs))
        states.append(tuple(pros))
    elif states_allowed:
        states.extend(states_allowed)
    else:
        states.extend(get_all_states(subs)+get_all_states(pros))
    if states_add:
        states.extend(states_add)
    # remove duplicates and double sort
    states = sorted(set([tuple(sorted(s)) for s in states]))

    state2term = OD([((), '1')])
    for state in states:
        term = '*'.join(['(%s/%s)'%(spid, spid2KM[spid]) for spid in state])
        if term == '':
            term = '1'
        term = str(sympy.simplify(term))
        state2term[state] = term
    if one_step:
        state2term[tuple(subs)] = '%s/KM_%s_f'%('*'.join(subs), rxnid)
        state2term[tuple(pros)] = '%s/KM_%s_b'%('*'.join(pros), rxnid)

    if KIcs:  # add the competitive-inhibitor bound states
        for spid in KIcs.keys():
            term = '(%s/KIc_%s_%s)'%(spid, rxnid, spid)
            state2term[tuple([spid])] = term

    #for (state, term) in state2term.items():
    #    state2term[state] = str(sympy.simplify(term))

    Vf, Vb = 'Vf_%s'%rxnid, 'Vb_%s'%rxnid
    if Keq:  # use Haldane's relation to cancel Vb
        # Haldane's relation says at equilibrium:
        # Vf*subs/KMs_sub = Vb*pros/KMs_pro; therefore:
        # pros/subs = Keq = Vf*KMs_pro/(Vb*KMs_sub),
        # Vb = Vf*KMs_pro/(KMs_sub*Keq)
        Keq = 'Keq_%s'%rxnid
        if one_step:
            KMs_f = 'KM_%s_f'%rxnid
        else:
            KMs_f = '(' + '*'.join(KMs_sub) + ')'
        numerator = '(%s/%s*(%s-%s/%s))'%(Vf, KMs_f, '*'.join(subs),
                                          '*'.join(pros), Keq)
    else:
        numerator = '(%s*%s - %s*%s)'%(Vf, state2term[tuple(subs)],
                                       Vb, state2term[tuple(pros)])
    if not reversible:
        numerator = '(%s*%s)'%(Vf, state2term[tuple(subs)])
    denominator = '(' + '+'.join(state2term.values()) + ')'
    ratelaw = '(%s/%s)'%(numerator, denominator)

    if KIus:
        for spid in KIus.keys():
            term = '%s/KIu_%s_%s'%(spid, rxnid, spid)
            ratelaw = '%s*(1/(1+%s))'%(ratelaw, term)
    if KAs:
        for spid in KAs.keys():
            term = '%s/KA_%s_%s'%(spid, rxnid, spid)
            ratelaw = '%s*(%s/(1+%s))'%(ratelaw, term, term)

    return ratelaw

def add_reaction_MM(net, id, Vs, stoich, KMs, reversible=True,
                    Keq=None, deltaG=None, KIcs=None, KIus=None, KAs=None,
                    one_step=False, duplicate_error=True,
                    param_optimizable=True,
                    states_allowed=None, states_add=None):
    """
    Requires the species to be in the network
    """
    rxnid = id

    # check if the species are already in the network

    # add the parameters
    for direction, paramval in Vs.items():
        if isinstance(paramval, tuple):
            paramval = paramval[0]
        paramid = 'V' + direction + '_' + rxnid
        net.add_parameter(paramid, paramval, is_optimizable=True)
    if Keq or deltaG is not None:
        if deltaG is not None:
            R = 8.314
            T = 310.15  # 273.15+37
            Keq = np.exp(-deltaG * 1e3 / R / T)
        #if isinstance(Keq, tuple):
        #    paramval = Keq[0]
        net.add_parameter('Keq_'+rxnid, Keq, is_optimizable=False)
    for spid, paramval in KMs.items():
        if isinstance(paramval, tuple):
            paramval = paramval[0]
        paramid = 'KM_' + rxnid + '_' + spid
        net.add_parameter(paramid, paramval, is_optimizable=True)
    if KIcs is not None:
        for spid, paramval in KIcs.items():
            if isinstance(paramval, tuple):
                paramval = paramval[0]
            paramid = 'KIc_' + rxnid + '_' + spid
            net.add_parameter(paramid, paramval, is_optimizable=True)
    if KIus is not None:
        for spid, paramval in KIus.items():
            if isinstance(paramval, tuple):
                paramval = paramval[0]
            paramid = 'KIu_' + rxnid + '_' + spid
            net.add_parameter(paramid, paramval, is_optimizable=True)
    if KAs is not None:
        for spid, paramval in KAs.items():
            if isinstance(paramval, tuple):
                paramval = paramval[0]
            paramid = 'KA_' + rxnid + '_' + spid
            net.add_parameter(paramid, paramval, is_optimizable=True)

    # get the rate law
    ratelaw = get_ratelaw_MM(rxnid=rxnid, stoich=stoich, reversible=reversible,
                             one_step=one_step, Keq=Keq,
                             KIcs=KIcs, KIus=KIus, KAs=KAs,
                             states_allowed=states_allowed,
                             states_add=states_add)
    # add the reaction
    net.addReaction(rxnid, stoich, kineticLaw=ratelaw)
    net.reactions.get(rxnid).kinetics = 'MichaelisMenten'

def add_reaction_SMM(net, id, Vs, stoich_or_eqn, KMs, reversible=True,
                     Keq=None, deltaG=None, KIcs=None, KIus=None, KAs=None,
                     duplicate_error=True, param_optimizable=True):
    """
    SMM: standard Michaelis-Menten kinetics, referring to quasi-equilibrium
         and random-order assumptions
    """
    if isinstance(stoich_or_eqn, 'str'):
        stoich = eqn2stoich(stoich_or_eqn)
    else:
        stoich = stoich_or_eqn

def add_transport_MM(net, rxnid, stoich, V, KM, reversible=True):
    """
    Transporter of Michaelis-Menten kinetics with the same V and KM.
    """
    id_V = 'V_' + rxnid
    id_sub = get_substrates(stoich, multi=False)[0]
    id_pro = get_products(stoich, multi=False)[0]
    spid = min([id_sub, id_pro], key=len)  # the shorter one is the spid
    id_KM = 'KM_%s_%s'%(rxnid, spid)
    if reversible:
        ratelaw = '%s/%s*(%s-%s)/(1+%s/%s+%s/%s)'%(id_V, id_KM, id_sub, id_pro,
                                                   id_sub, id_KM, id_pro, id_KM)
    else:
        raise NotImplementedError
        # there is no id_pro
        #ratelaw = '%s*%s/%s/(1+%s/%s)'%(id_V, id_sub, id_KM, id_sub, id_pro)
    net.add_parameter(id_V, V, is_optimizable=True)
    net.add_parameter(id_KM, KM, is_optimizable=True)
    net.addReaction(rxnid, stoich, kineticLaw=ratelaw)

def get_ratelaw_MA(rxnid, stoich, reversible, Keq):
    subs = get_substrates(stoich, multi=True)
    prods = get_products(stoich, multi=True)
    kf = 'kf_' + rxnid
    kb = 'kb_' + rxnid
    if Keq:
        Keq = 'Keq_' + rxnid
    if not reversible:
        ratelaw = '%s*%s' % (kf, '*'.join(subs))
    else:
        if Keq:
            kb = '(%s/%s)' % (kf, Keq)
        ratelaw = '%s*%s-%s*%s' % (kf, '*'.join(subs),
                                   kb, '*'.join(prods))
    return ratelaw

def add_reaction_MA(net, id, ks, stoich, reversible=True, Keq=None,
                    duplicate_error=True):
    """
    """
    rxnid = id

    # check if the species are already in the network
    for spid in stoich.keys():
        if spid not in net.species.keys():
            raise StandardError("Error when adding reaction %s:\
                                 species %s has not been added yet."
                                    % (rxnid, spid))
    # add the parameters
    for direction, paramval in ks.items():
        if isinstance(paramval, tuple):
            paramval = paramval[0]
        paramid = 'k' + direction + '_' + rxnid
        net.add_parameter(paramid, paramval, is_optimizable=True)
    if Keq:
        net.add_parameter('Keq_'+rxnid, Keq, is_optimizable=False)
    # get the rate law
    ratelaw = get_ratelaw_MA(rxnid=rxnid, stoich=stoich,
                             reversible=reversible, Keq=Keq)
    # add the reaction
    net.addReaction(rxnid, stoich, kineticLaw=ratelaw)
    net.reactions.get(rxnid).kinetics = 'MassAction'

def get_ratelaw_MWC():
    pass

def add_reaction_MWC():
    pass

class IrreversibleMultisubstrateMichaelisMenten(Reaction):
    """

    """

    def __init__(self, id, stoichiometry, info_inhibitors=None, activators=None):
        """
        Parameters
        ----------
        info_inhibitors, a dict
            example:
            [{'inhibitor': 'I1', 'substrate': 'S1', 'mode': 'competitive'},
             {'inhibitor': 'I1', 'substrate': 'S1', 'mode': 'uncompetitive'},
             {'inhibitor': 'I2', 'substrate': 'S2', 'mode': 'competitive'},
             {'inhibitor': 'I3', 'substrate': 'S1', 'mode': 'competitive'}
            ]
            Let:
                id = 13
                substrates = ['S1', 'S2']
            Then:
                kineticLaw = 'Vm_13*S1*S2/((Km_13_S1*(1+I1/Kic_13_S1_I1+I3/Kic_13_S1_I3)+
                              S1*(1+I1/Kiu_13_S1_I1))*(Km_13_S2*(1+I2/Kic_13_S2_I2)+S2))'
        activators, a dict
            example: to be implemented.
        """
        str_Vm = 'Vm_' + id
        substrates = get_substrates(stoichiometry)
        kineticLaw_numerator = str_Vm + '*' + '*'.join(substrates)
        kineticLaw_denominator_components = []
        for substrate in substrates:
            str_Km = 'Km_' + id + '_' + substrate
            str_concn = substrate
            if info_inhibitors:  # if the reaction has inhibitors
                for info in info_inhibitors:
                    if isinstance(info, tuple):
                        inhibitor, substrate_inhibited, mode = info
                    else:
                        substrate_inhibited = info['substrate']
                        inhibitor = info['inhibitor']
                        mode = info['mode']
                    if substrate == substrate_inhibited:
                        if mode == 'competitive' or mode == 'c':
                            str_Ki = 'Kic_' + id + '_' + substrate + '_' + inhibitor
                            if not str_Km.endswith(')'):
                                str_Km = str_Km + '*(1+' + inhibitor + '/' +  str_Ki + ')'
                            else:
                                str_Km = str_Km[:-1] + '+' + inhibitor + '/' + str_Ki + ')'
                        else:  # uncompetitive inhibition
                            str_Ki = 'Kiu_' + id + '_' + substrate + '_' + inhibitor
                            if not str_concn.endswith(')'):
                                str_concn = str_concn + '*(1+' + inhibitor + '/' +  str_Ki + ')'
                            else:
                                str_concn = str_concn[:-1] + '+' + inhibitor + '/' + str_Ki + ')'
            else:  # if the reaction has no inhibitors
                pass
            kineticLaw_denominator_components.append('('+str_Km+'+'+str_concn+')')
        kineticLaw_denominator = '*'.join(kineticLaw_denominator_components)
        kineticLaw = kineticLaw_numerator + '/(' + kineticLaw_denominator + ')'
        Reaction.__init__(self, id, stoichiometry, kineticLaw)

class ReversibleMassAction(Reaction):
    """
    """
    def __init__(self, id, stoichiometry):
        """
        """
        str_substrates = '*'.join(['pow('+sp+','+str(abs(stoichiometry[sp]))+')'
                                   for sp in stoichiometry.keys() if stoichiometry[sp]<0])
        str_products = '*'.join(['pow('+sp+','+str(stoichiometry[sp])+')'
                                 for sp in stoichiometry.keys() if stoichiometry[sp]>0])
        kineticLaw = 'k_'+id+'*('+str_substrates+'-'+str_products+'/'+'Ke_'+id+')'
        Reaction.__init__(self, id, stoichiometry, kineticLaw)

def add_diseq_variables(net, rxnid2dG0=None,
                        add_Q=False, add_dG=False, add_g=True,
                        skip_no_dG0=True, T=25):
    """
    Add disequilibrium variables

    dG = deltaG
    dG0 = deltaG0
    g = dG/RT
    g0 = dG0/RT

    Input:
        T: temperature in celsius degrees (~25 for room temperature and ~37 for
            human body)

    """
    net = net.copy()

    for rxnid, rxn in net.reactions.items():
        subs = get_substrates(rxn.stoichiometry)
        pros = get_products(rxn.stoichiometry)
        str_sub = '*'.join(subs)
        str_pro = '*'.join(pros)
        str_Q = '(%s)/(%s)' % (str_pro, str_sub)
        id_Q = 'Q_' + rxnid

        if add_Q:
            net.add_parameter(id_Q)
            net.add_assignment_rule(id_Q, str_Q)

        RT = 8.315 * (T + 273.15) / 1000
        ## get deltaG0 (G0)
        if rxnid2dG0 and rxnid in rxnid2dG0:
            dG0 = rxnid2dG0[rxnid]
        elif hasattr(rxn, 'deltaG0'):
            dG0 = rxn.deltaG0
        elif hasattr(rxn, 'dG0'):
            dG0 = rxn.dG0
        else:
            if skip_no_dG0:
                continue
            else:
                raise Exception("deltaG0 for rxn %s undefined." % rxnid)

        if add_dG:
            id_dG = 'dG_' + rxnid
            if add_Q:
                str_dG = '%.3f + %.3f * log(%s)' % (dG0, RT, id_Q)
            else:
                str_dG = '%.3f + %.3f * log(%s)' % (dG0, RT, str_Q)
            net.add_parameter(id_dG)
            net.add_assignment_rule(id_dG, str_dG)

        if add_g:
            id_g = 'g_' + rxnid
            if add_Q:
                str_g = '%.3f + log(%s)' % (dG0/RT, id_Q)
            else:
                str_g = '%.3f + log(%s)' % (dG0/RT, str_Q)
            net.add_parameter(id_g)
            net.add_assignment_rule(id_g, str_g)

    return net


from io import StringIO
import bit
import module
import stream

class reg(module.module):
	def __init__(self, width=1, rst_value=None):
		self.rst_value = 0 if rst_value is None else rst_value
		self.w = width
		super().__init__()

	def width(self):
		return self.w

	def name(self):
		return 'register_%d_%d' % (self.width(), self.rst_value)

	def impl(self):
		self.clk = bit.input(bit.bit(1))
		self.rst_l = bit.input(bit.bit(1))
		self.en = bit.input(bit.bit(1))
		self.clear = bit.input(bit.bit(1))
		self.D = bit.input(bit.bit(self.width()))
		self.Q = bit.output(bit.bit(self.width()))

	def __inst_repr__(self, id):
		s = StringIO()
		f = stream.stream(s)
		f.write('register #(%d, %d) REG%s(' % (
			self.width(), self.rst_value, id))
		more_than_one = False
		indented = False
		decls = self.decls()
		for k in decls:
			p = decls[k]
			if p.dxn() is not None:
				if more_than_one:
					f.writenl(',', i=0)
					if not indented:
						f.indent()
						indented = True
				f.write('.%s(%s)' % (self.cxnport(p.name()), self.cxnname(p.name())))
				more_than_one = True

		f.unindent()
		f.writenl(');')

		return s.getvalue()

	def __repr__(self):
		return ''


import json
import boto3

#getMyEvents
def lambda_handler(event, context):
    dynamodb = boto3.resource("dynamodb")
    table = dynamodb.Table("Events")
    try:
        user = event["requestContext"]["authorizer"]["claims"]
        email = user["email"]
    except Exception as inst:
        print("")
        return {
            "statusCode": 400,
            "headers": {
                "Access-Control-Allow-Origin": "*"
            },
            "body": json.dumps("Error getting the user of request\n\n" + str(type(inst)) + "\n\n" + str(inst.args))
        }

    #search in db for email as organiser
    try:
        response = table.scan()
        all_items = response["Items"]
        items = []
        for item in all_items:
            try:
                if item["organizer"] == email:
                    items.append(item)
            except:
                items.append(item)
        return {
            'statusCode': 200,
            "headers": {
                    "Access-Control-Allow-Origin": "*"
                },
            'body': json.dumps(items)
        }
    except Exception as inst:
        return {
            "statusCode": 500,
            "headers": {
                "Access-Control-Allow-Origin": "*"
            },
            "body": json.dumps("Error getting events from db with given user as organizer\n\n" + str(type(inst)) + "\n\n" + str(inst.args))
    }


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

from .init import init_uniform, init_bn

class FC(nn.Module):
    """Applies a linear transformation to the incoming data
    optionally followed by batch normalization and relu activation

    Attributes:
        conv (nn.Module): convolution module
        bn (nn.Module): batch normalization module
        relu (bool): whether to activate by relu

    """

    def __init__(self, in_channels, out_channels,
                 relu=True, bn=True, bn_momentum=0.1):
        super(FC, self).__init__()

        self.fc = nn.Linear(in_channels, out_channels, bias=(not bn))
        self.bn = nn.BatchNorm1d(out_channels, momentum=bn_momentum) if bn else None
        self.relu = relu

        self.init_weights()

    def forward(self, x):
        x = self.fc(x)
        if self.bn is not None:
            x = self.bn(x)
        if self.relu:
            x = F.relu(x, inplace=True)
        return x

    def init_weights(self):
        """default initialization"""
        init_uniform(self.fc)
        if self.bn is not None:
            init_bn(self.bn)


import enum

class ComparisonType(str, enum.Enum):
    EQUAL = 0
    NOTEQUAL = 1
    LESS = 2
    GREATER = 3
    GREATER_EQUAL = 4
    LESS_EQUAL = 5
    CONTAINS = 6

data_type_to_comparison_type = {
    bool: [
        ComparisonType.EQUAL,
        ComparisonType.NOTEQUAL
    ],
    int: [
        ComparisonType.EQUAL,
        ComparisonType.NOTEQUAL,
        ComparisonType.LESS,
        ComparisonType.GREATER,
        ComparisonType.GREATER_EQUAL,
        ComparisonType.LESS_EQUAL
    ],
    float: [
        ComparisonType.EQUAL,
        ComparisonType.NOTEQUAL,
        ComparisonType.LESS,
        ComparisonType.GREATER,
        ComparisonType.GREATER_EQUAL,
        ComparisonType.LESS_EQUAL
    ],
    str: [
        ComparisonType.EQUAL,
        ComparisonType.NOTEQUAL,
        ComparisonType.CONTAINS,
    ],
    list: [
        ComparisonType.CONTAINS,
    ]
}

def equal_func(a, b) -> bool:
    return a == b

def not_equal_func(a, b) -> bool:
    return a != b

def less_func(a, b) -> bool:
    return a < b

def less_equal_func(a, b) -> bool:
    return a <= b

def greater_func(a, b) -> bool:
    return a > b

def greater_equal_func(a, b) -> bool:
    return a >= b

def contains_func(l: list, a) -> bool:
    return a in l

comparison_type_functions = {
    ComparisonType.EQUAL: equal_func,
    ComparisonType.NOTEQUAL: not_equal_func,
    ComparisonType.LESS: less_func,
    ComparisonType.GREATER: greater_func,
    ComparisonType.GREATER_EQUAL: greater_equal_func,
    ComparisonType.LESS_EQUAL: less_equal_func,
    ComparisonType.CONTAINS: contains_func,
}

comparison_type_to_sql_operator = {
    ComparisonType.EQUAL: "==",
    ComparisonType.NOTEQUAL: "==",
    ComparisonType.LESS: "<",
    ComparisonType.GREATER: ">",
    ComparisonType.GREATER_EQUAL: ">=",
    ComparisonType.LESS_EQUAL: "<=",
}


#!/usr/bin/python
# -*- coding: utf-8 -*-

import asyncio
import functools

def greeting(welcome):  # no async declare here
    print(welcome)

async def main(loop):
    loop.call_soon_threadsafe(functools.partial(greeting, "hello asyncio"))
    loop.call_soon(functools.partial(greeting, "hello asyncio"))

    # Most asyncio functions don't accept keyword. If you want to pass keywords
    # to your callback, use functions.partial(). For example,
    # loop.call_soon(functions.partial(print, "Hello", flush=True))
    # will call print("Hello, flush=True")

    # Note: functools.partial() is better than lambda functions, because
    # asyncio can inspect functions.partial() object to display parameters
    # in debug mode, whereas lambda functions have a poor representation.

loop = asyncio.get_event_loop()
loop.run_until_complete(main(loop))
loop.close()

# https://docs.python.org/3/library/asyncio-eventloop.html#calls


import matplotlib.pyplot as plt
import tensorflow as tf
import numpy as np

# Define placeholders for input and ouput values
x = tf.placeholder(tf.float32, [None, 1], name='x')
y = tf.placeholder(tf.float32, [None, 1], name='y')

# Define fully-connected layer with 10 hidden units
h = tf.layers.dense(x, 10, activation=tf.nn.relu)

# Define fully-connected layer with 20 hidden units
h = tf.layers.dense(h, 20, activation=tf.nn.relu)

# Define fully-connected layer with 10 hidden units
h = tf.layers.dense(h, 10, activation=tf.nn.relu)

# Define fully-connected layer to single ouput prediction
pred = tf.layers.dense(h, 1, activation=None)

# Define loss function
loss = tf.reduce_mean(tf.pow(pred - y, 2))

# Define optimizer
optim = tf.train.AdamOptimizer(learning_rate=0.001).minimize(loss)

# Define variable initializer
init = tf.global_variables_initializer()

# Initialize TensorFlow session
with tf.Session() as sess:

    # Initialize variables
    sess.run(init)

    # Iterate through 10000 training steps
    for n in range(0,10000):

        # Create artificial data with mini-batch size 100
        x_batch = np.pi/2 * np.random.normal(size=[100, 1])
        y_batch = np.sin(x_batch)

        # Run optimization operation for current mini-batch
        fd = {x: x_batch, y: y_batch}
        sess.run(optim, feed_dict=fd)

    # Plot predicted and true values for qualitative evaluation
    eval_pts = np.expand_dims(np.linspace(-np.pi/2, np.pi/2, 1000) , 1)
    predictions = sess.run(pred, feed_dict={x: eval_pts})
    true_values = np.sin(eval_pts)
    plt.plot(eval_pts[:,0], predictions[:,0], 'b')
    plt.plot(eval_pts[:,0], true_values[:,0], 'r')
    plt.show()


#!/usr/bin/python3
import os
import re
import json
from sys import argv

with open(argv[1]) as fd:
    content = fd.read()
    content = re.sub(r'\n', '', content)
    obj = json.loads(content)
    for cell in obj['cells']:
        if cell['cell_type'] == 'code':
            cell['execution_count'] = None
            cell['outputs'] = []
            if (len(cell['source']) > 0 and cell['source'][0].startswith('##keep')):
                continue
            cell['source'] = []
    print(json.dumps(obj))


#!/usr/bin/env python
# -*- coding:utf-8 -*-
# @brief: publish module to maven
# @date:   2023.08.10 14:40:50

import os
import sys

g_this_file = os.path.realpath(sys.argv[0])
g_this_path = os.path.dirname(g_this_file)
sys.path.append(os.path.dirname(g_this_path))

from utils.utils_cmn import CmnUtils
from utils.utils_logger import LoggerUtils
from utils.utils_file import FileUtils
from utils.utils_import import ImportUtils
from basic.git import BasicGit
from basic.arguments import BasicArgumentsValue
from basic.space import BasicSpace
from basic.gradle import BasicGradle

g_wing_path = ImportUtils.initEnv()

# --------------------------------------------------------------------------------------------------------------------------
class ProjectCollector:
    def __init__(self, spacePath):
        self.mSpacePath = spacePath
        self.space = {}
        self.projects = []

    def doCollect(self):
        self.__doCollectSpace__()
        self.__doCollectModule__()

        jdata = {
            'space': self.space,
            'project': self.projects
        }
        FileUtils.saveJsonToFile(self.mSpacePath + '/out/project.json', jdata)

    def __doCollectSpace__(self):
        """
          "space": {
            "group": "xxx",
            "branch": "main",
            "manifest": "admin.xml"
            "remote": "wing"
          }
        """
        space = BasicSpace(self.mSpacePath)
        path = self.mSpacePath + '/.wing/manifests'
        branch, sname = ProjectCollector.getGitInfo(path)
        assert not CmnUtils.isEmpty(sname), "No manifests found !"
        self.space["name"] = "manifests"
        self.space["path"] = path
        self.space["branch"] = branch
        self.space["remote"] = sname
        self.space["group"] = space.getGroup()
        self.space["manifest"] = space.getManifest()

    def __doCollectModule__(self):
        """
          "${module name}": {
            "branch": "${git branch name}",
            "type": "${project type}"
            "remote": "$git remote name"
          }
        """
        self.scanPath(self.mSpacePath, 1)

    def doParseBase(self, d, pname, branch, sname):
        pitem = {
            "name": pname,
            "path": d,
            "branch": branch,
            "remote": sname
        }
        return pitem

    def doParseWithAndroid(self, path, pitem):
        """
          "${module name}": {
            "type": "aar"
          }
        """
        gradle = BasicGradle(path)
        if not gradle.isValid(): return False

        pitem['type'] = "gradle"
        modules = []
        pitem['module'] = modules
        for module in gradle.getModules():
            LoggerUtils.w('    :' + module)
            mm = {}
            mm['name'] = module
            mode = gradle.getMode(module)
            if None != mode: mm['mode'] = mode
            modulePath = gradle.getModulePath(module)
            if os.path.isfile(modulePath + '/.checkignore'): mm['ignore-check'] = True
            modules.append(mm)
        return True

    def scanPath(self, path, level):
        ll = os.listdir(path)
        for l in ll:
            if l.startswith('.'): continue
            d = path + os.sep + l
            if not os.path.isdir(d): continue
            branch, sname = ProjectCollector.getGitInfo(d)
            if not CmnUtils.isEmpty(sname):
                pname = d[len(self.mSpacePath) + 1:]
                LoggerUtils.light(pname)
                pitem = self.doParseBase(d, pname, branch, sname)
                self.projects.append(pitem)
                if self.doParseWithAndroid(d, pitem): continue
                # TODO add more parser here ...
                continue
            if 3 < level: continue
            self.scanPath(d, level + 1)

    @staticmethod
    def getGitInfo(projPath):
        git = BasicGit(projPath)
        return git.getCurrentBranch(), git.getServerName()

def run():
    """
    wing -project
    """
    argv = BasicArgumentsValue()
    envPath, spacePath, projPath, cmd = argv.get(0), argv.get(1), argv.get(2), argv.get(3)
    ProjectCollector(spacePath).doCollect()

if __name__ == "__main__":
    run()


# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.

import argparse
import os
import shutil
import sys

from mozbuild.util import ensureParentDir

def main(argv):
    parser = argparse.ArgumentParser(description="Merge l10n files.")
    parser.add_argument("--output", help="Path to write merged output")
    parser.add_argument("--ref-file", help="Path to reference file (en-US)")
    parser.add_argument("--l10n-file", help="Path to locale file")

    args = parser.parse_args(argv)

    from compare_locales.compare import ContentComparer, Observer
    from compare_locales.paths import File

    cc = ContentComparer([Observer()])
    cc.compare(
        File(args.ref_file, args.ref_file, ""),
        File(args.l10n_file, args.l10n_file, ""),
        args.output,
    )

    ensureParentDir(args.output)
    if not os.path.exists(args.output):
        src = args.l10n_file
        if not os.path.exists(args.l10n_file):
            src = args.ref_file
        shutil.copy(src, args.output)

    return 0

if __name__ == "__main__":
    main(sys.argv[1:])


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

__author__ = 'sobolevn'

from utils import get_input_function

class Storage(object):  # storage = Storage()
    obj = None

    items = None

    done = None

    @classmethod
    def __new__(cls, *args):
        if cls.obj is None:
            cls.obj = object.__new__(cls)
            cls.items = []
            cls.done = None
        return cls.obj

class BaseItem(object):
    def __init__(self, heading, done=''):
        self.heading = heading
        self.done = done

    def __repr__(self):
        return self.__class__

    @classmethod
    def construct(cls):
        raise NotImplemented()

class ToDoItem(BaseItem):
    def __str__(self):
        return 'ToDo: {}{}'.format(
            self.heading,
            self.done,
        )

    @classmethod
    def construct(cls):
        input_function = get_input_function()
        heading = input_function('Input heading: ')
        done = ' -'
        return ToDoItem(heading, done)

class ToBuyItem(BaseItem):
    def __init__(self, heading, price, done=''):
        super(ToBuyItem, self).__init__(heading)
        self.price = price
        self.done = done

    def __str__(self):
        return 'ToBuy: {} for {}{}'.format(
            self.heading,
            self.price,
            self.done,
        )

    @classmethod
    def construct(cls):
        input_function = get_input_function()
        heading = input_function('Input heading: ')
        price = input_function('Input price: ')
        done = ' -'
        return ToBuyItem(heading, price, done)

class ToReadItem(BaseItem):
    def __init__(self, heading, url, done=''):
        super(ToReadItem, self).__init__(heading)
        self.url = url
        self.done = done

    def __str__(self):
        return 'ToRead: {} for {}{}'.format(
            self.heading,
            self.url,
            self.done
        )

    @classmethod
    def construct(cls):
        input_function = get_input_function()
        heading = input_function('Input heading: ')
        url = input_function('Input URL: ')
        done = ' -'
        return ToReadItem(heading, url, done)


def f(u, t):
    return [u[1], 3.*(1. - u[0]*u[0])*u[1] - u[0]]

u0 = [2.0, 0.0]
import odespy, numpy, matplotlib.pyplot as plt

for method in odespy.Lsode, odespy.DormandPrince, odespy.Vode:
    solver = method(f, rtol=0.0, atol=1e-6,
                    adams_or_bdf='adams', order=10)
    solver.set_initial_condition(u0)
    t_points = numpy.linspace(0, 30, 150)
    u, t = solver.solve(t_points)

    plt.plot(t, u[:,0])
    plt.hold('on')
plt.show()


from scipy.stats import norm
import matplotlib.pyplot as plt
import numpy as np

plt.rcdefaults()
values = [ 12, 55, 4, 32, 14]
colors = ['r', 'g', 'b', 'c', 'm']
explode = [0, 0, 0.2, 0, 0]
labels = ['India', 'United States', 'Rusia', 'China', 'Europe']
plt.pie(values, colors=colors, labels=labels, explode=explode)
plt.title('Nacionalidad Trabajadores')
plt.savefig("/home/hadoop/SCProjects/0_SCProjects_github.com_SCelisV/python/SC.Code/pue-cca175/pictures/figura10_tarta.png", format="png")
plt.show()


# -*- coding: utf-8 -*-
import scrapy
from ..items import ItcastItem

class DemoSpider(scrapy.Spider):
    # 识别名称，必须是唯一的
    name = 'Demo'
    # 搜索的域名范围，也就是爬虫的约束区域
    allowed_domains = ['itcast.cn']
    start_urls = [
        'http://www.itcast.cn/channel/teacher.shtml',
    ]
    def parse(self, response):
        # 爬取网页源码保存到teacher.html
        # filename = "teacher.html"
        # html = open(filename, 'wb+')
        # html.write(response.body)
        items = []
        #

        #
吴老师

        #
高级讲师

        #
有多年PHP开发和项目管理经验，开发过聊天在线系统、客户关系管理系统、智能SEM竞价系统等项目，精通PHP、mysql、linux、javascript、html5、css3等主流的web开发技术，讲课风格风趣幽默有激情，善于启发学生自我思考来解决问题。

        #

        for each in response.xpath("//div[@class='li_txt']"):
        # 将得到的数据封装到一个 `ItcastItem` 对象
            item = ItcastItem()
        # extract()方法返回的都是unicode字符串
            name = each.xpath("h3/text()").extract()
            title = each.xpath("h4/text()").extract()
            info = each.xpath("p/text()").extract()
        # xpath返回的是包含一个元素的列表
            item['name'] = name[0]
            item['level'] = title[0]
            item['info'] = info[0]
            items.append(item)
        # 直接返回最后数据
        return items
# scrapy startproject Demo
# scrapy crawl Demo
# scrapy crawl Demo -o teachers.csv


import logging
from clarity_ext.domain.validation import ValidationType, UsageError
from clarity_ext.service.step_logger_service import AggregatedStepLoggerService

class ValidationService:

    def __init__(self, step_logger_service, logger=None):
        self.logger = logger or logging.getLogger(__name__)
        self.step_logger_service = AggregatedStepLoggerService(step_logger_service)
        self.warning_count = 0
        self.error_count = 0
        self.messages = set()

    def add_separate_warning_step_log(self, step_logger_service):
        self.step_logger_service.warnings_step_logger_service = step_logger_service

    def add_separate_error_step_log(self, step_logger_service):
        self.step_logger_service.errors_step_logger_service = step_logger_service

    @property
    def log_file_names(self):
        return self.step_logger_service.log_file_names

    def handle_validation(self, results, tailored_error_message=None):
        """
        Pushes validation results to the logging framework
        """
        if len(results) > 0:
            self._log_debug("Validation errors, len = {}".format(len(results)))
            for result in results:
                self._log_debug(result)
                self.handle_single_validation(result)
        # If any of the validation results were errors, raise an exception:
        if any(result for result in results if result.type == ValidationType.ERROR):
            msg = tailored_error_message if tailored_error_message is not None \
                else "Errors during validation. See the step log for further details."
            raise UsageError(msg, results)

    def handle_single_validation(self, result):
        msg_row = "{}".format(result)
        if msg_row in self.messages:
            # Quick fix: the messages per transfers are often duplicated. Skip adding them several times to the log.
            return
        self.messages.add(msg_row)
        self._log_debug("{}".format(msg_row))

        if result.type == ValidationType.ERROR:
            self.step_logger_service.error(msg_row)
            self.error_count += 1
        elif result.type == ValidationType.WARNING:
            self.step_logger_service.warning(msg_row)
            self.warning_count += 1
        else:
            self.step_logger_service.log(msg_row)

    def _log_debug(self, msg):
        if self.logger is not None:
            self.logger.debug(msg)


import json
import random

from django.conf import settings
from django.core.mail import send_mail
from django.http import HttpResponse, Http404
from django.template import loader
from django.views.generic import View

from pmo2015.forms import MessageForm, BattleForm
from pmo2015.models import MainComment, Player, Vote

def _return_me(error_code, **kwargs):
    kwargs["error"] = error_code
    return kwargs

def _guestbook(request, *args, **kwargs):
    form = MessageForm(request.POST)
    if not form.is_valid():
        if 'captcha' in form.errors:
            return _return_me(2)
        return _return_me(1)
    ip_address = request.META.get("REMOTE_ADDR")
    if ip_address is None:
        return _return_me(-1)
    try:
        MainComment.create(
            nickname=form.cleaned_data['nickname'],
            content=form.cleaned_data['message'],
            email=form.cleaned_data['email'],
            ip_address=ip_address
        )
    except:
        return _return_me(-1)
    return _return_me(0)

def _battle(request, *args, **kwargs):
    form = BattleForm(request.POST)
    if not form.is_valid():
        if 'captcha' in form.errors:
            return _return_me(4)
        if 'taobao' in form.errors:
            return _return_me(5)
        return _return_me(1, errors=form.errors)

    player_name = form.cleaned_data['nickname']
    email = form.cleaned_data['email']
    taobao_id = form.cleaned_data['taobao']
    team = form.cleaned_data['team']
    team = (random.choice(Vote.TEAM_CHOICES)[0] if team == 'random' else team).upper()
    if team not in {Vote.TEAM_AQUA, Vote.TEAM_MAGMA}:
        return _return_me(-1)
    ip_address = request.META.get("REMOTE_ADDR")
    if ip_address is None:
        return _return_me(-1)
    elif any(Player.objects.filter(email=email, status=0)) or any(Player.objects.filter(email=email, status=1)):
        return _return_me(2)
    else:
        try:
            player = Player.create(
                player_name=player_name,
                email=email,
                taobao_id=taobao_id,
                signup_ip=ip_address,
                team=team
            )
        except AssertionError:
            return _return_me(3)
        except:
            return _return_me(-1)
    send_mail(
        '%s对战报名' % settings.EMAIL_SUBJECT_PREFIX, "",
        settings.EMAIL_HOST_USER, [email], fail_silently=False,
        html_message=loader.get_template('pmo2015/mails/battle_signup.html').render({
            'player': player,
            'base_url': settings.BASE_URL,
            'weibo_url': settings.WEIBO_URL,
            'contact_mail': settings.CONTACT_EMAIL
        }),
    )
    return _return_me(0)

def _vote(request, *args, **kwargs):
    choice = request.POST.get("team").upper()
    ip_address = request.META.get("REMOTE_ADDR")
    if not all((choice, ip_address)) or choice not in {Vote.TEAM_AQUA, Vote.TEAM_MAGMA}:
        return _return_me(-1)
    if Vote.objects.filter(ip_address=ip_address).count() > 0 or request.session.get('vote'):
        return _return_me(1)
    vote = Vote.objects.create(
        choice=choice,
        ip_address=ip_address
    )
    request.session['vote'] = vote.id
    return _return_me(0, vote=0 if choice == Vote.TEAM_AQUA else 1)

_api_list = {
    'guestbook': _guestbook,
    'battle': _battle,
    'vote': _vote
}

class ApiView(View):

    @staticmethod
    def post(request, sub=None, *args, **kwargs):
        if sub not in _api_list:
            raise Http404
        func = _api_list[sub]
        return HttpResponse(json.dumps(func(
            request, *args, **kwargs
        )), content_type='application/json')

    @staticmethod
    def get(*args, **kwargs):
        raise Http404


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

from sanic import Sanic
from sanic.log import logger

app = Sanic("card_games")

app.static('/static', './static')

@app.listener('before_server_start')
async def before_server_start(app, loop):
    app.ctx.rooms = {}
    """
    # base no db
    app.ctx.rooms = {
        'room1': {
            'users': {
                'user1': {
                    'name': 'user1',
                    'ws': websocket,
                    'room': 'room1',
                    'ready': False,
                    'cards': [],
                    'landlord': False,
                    'score': 0,
                    'type': 'player',   # player, npc
                    'master': False,
                },
            },
            'status': 'waiting',    # waiting, playing, end
            'landlord': None,
            'last_cards': [],
            'last_user': None,
        }
    }
    """

@app.route('/')
async def index(request):
    return await app.send_file('./static/index.html')

@app.route('/ddz')
async def ddz(request):
    if not request.args.get('name'):
        # not login
        # TODO login(input name)
        return await app.send_file('./static/ddz.html')
    _room = request.args.get('room')
    if not _room:
        # login but not in room
        # TODO create room or join room(input room)
        return await app.send_file('./static/ddz.html')
    if not app.ctx.rooms.get(_room, None):
        # login but room not exist
        # TODO room list? or create room
        return await app.send_file('./static/ddz.html')

    return await app.send_file('./static/ddz.html', room=app.ctx.rooms[_room])

@app.route('/ddz/create')
async def ddz_create(request):
    """create room"""
    # TODO to websocket?
    _user = request.args.get('name')
    if not _user:
        # TODO login
        return await app.send_file('./static/ddz.html')

    # TODO room configs
    _room = 'room1'     # TODO create room
    if app.ctx.rooms.get(_room, None):
        # TODO room exist random room
        _room = _room + '1'

    app.ctx.rooms[_room] = {
        'users': {
            _user: {
                'name': _user,
                'ws': None,
                'room': _room,
                'ready': False,
                'cards': [],
                'landlord': False,
                'score': 0,
                'type': 'player',
                'master': True,
            },
        },
        'status': 'waiting',
        'landlord': None,
        'last_cards': [],
        'last_user': None,
    }

    return await app.send_file('./static/ddz.html', room=app.ctx.rooms[_room])

@app.websocket('/ddz/ws')
async def ddz_ws(request, ws):
    _room = request.args.get('room')
    _user = request.args.get('name')
    if not _room or not _user:
        # TODO login
        return

    if not app.ctx.rooms.get(_room, None):
        # TODO room not exist
        return

    if not app.ctx.rooms[_room]['users'].get(_user, None):
        # TODO user not exist
        return

    app.ctx.rooms[_room]['users'][_user]['ws'] = ws

    while True:
        data = await ws.recv()
        # TODO parse data
        # TODO do something
        # TODO send data to other users
        # TODO send data to self
        logger.info(data)

if __name__ == '__main__':
    app.run(host="0.0.0.0", port=8000, debug=True)


# Author: Niharika Singhal
#
# For license information, see LICENSE.txt

"""
Functions for estimating the parameters (alpha_t, sigma^2_t, beta^1_t, beta^2_t, beta^3_t) of the linear regression with
degree (LR-N)

References
----------
.. *Kobayashi and Lambiotte, ICWSM, pp. 191-200, 2016; Zhao et al., KDD, pp. 1513-1522, 2015*.
"""

import numpy as np
from scipy import linalg

def parameter_estimation_lr_n(follower_orig, total_follower_t, no_events, event_pred):
    """
    Fit the parameters of the linear regression with degree
    :param follower_orig: array, containing the number of followers of the original tweeting person for all tweets (d_0).
    :param total_follower_t: array, containing the cumulative number of followers at an observation time for all tweets (D(T)).
    :param no_events: array, containing the total number of retweets at an observation time (R(T) ) for all tweets.
    :param event_pred: array, containing the total number of retweets at a prediction time (R(t) ) for all tweets.
    :return: parameters of the linear regression with degree.
    """

    a = np.matrix([
        [len(follower_orig), sum(no_events), sum(total_follower_t), sum(follower_orig)],
        [sum(no_events), sum(no_events * no_events), sum(total_follower_t * no_events),
         sum(follower_orig * no_events)],
        [sum(total_follower_t), sum(no_events * total_follower_t), sum(total_follower_t * total_follower_t),
         sum(total_follower_t * follower_orig)],
        [sum(follower_orig), sum(no_events * follower_orig), sum(total_follower_t * follower_orig),
         sum(follower_orig * follower_orig)]
    ])

    # y1 = sum(event_pred)
    # y2 = sum([(no_events[i] * event_pred[i]) for i in range(len(event_pred))])
    # y3 = sum([(total_follower_t[i] * event_pred[i]) for i in range(len(event_pred))])
    # y4 = sum([(follower_orig[i] * event_pred[i]) for i in range(len(event_pred))])

    # same as above with numpy to avoid loops
    y1 = np.sum(event_pred, axis=0)
    y2 = np.sum((event_pred.T * no_events).T, axis=0)
    y3 = np.sum((event_pred.T * total_follower_t).T, axis=0)
    y4 = np.sum((event_pred.T * follower_orig.T).T, axis=0)

    if isinstance(y1, np.float64):
        theta = linalg.solve(a, [y1, y2, y3, y4])
        sigma_val = [(event_pred[i] - theta[0] - (theta[1] * no_events[i]) - (theta[2] * total_follower_t[i]) -
                      (theta[3] * follower_orig[i])) ** 2 for i in range(len(event_pred))]
        sigma_sq = sum(sigma_val) / len(event_pred)

        return [theta], [sigma_sq]

    else:
        theta = [linalg.solve(a, [y1[i], y2[i], y3[i], y4[i]]) for i in range(len(sum(event_pred)))]
        sigma_sq_list = []
        for j in range(len(theta)):
            val = [(event_pred[i][j] - theta[j][0] - (theta[j][1] * no_events[i]) -
                    (theta[j][2] * total_follower_t[i]) - (theta[j][3] * follower_orig[i])) ** 2
                   for i in range(len(no_events))]
            sigma_sq = sum(val) / len(no_events)
            sigma_sq_list.append(sigma_sq)

        return theta, sigma_sq_list


import time
import os
from pyspark import SparkConf
from pyspark import SparkContext
from pyspark.sql import SparkSession
from pyspark.sql.functions import from_json, avg, col, concat, desc, explode, lit, min, max, sum, count, split, udf, to_date, from_unixtime, datediff, when, countDistinct, date_add
from pyspark.sql.types import StructType, StructField, StringType, LongType, IntegerType, FloatType, DateType, TimestampType
from pyspark.sql.functions import explode, sum, split, col, expr, when
from pyspark.sql.utils import ForeachBatchFunction

from util.utils import add_date_field, Normalize_Data_ToUser, extract_past_N_days, Choose_Features, remove_invalid_record, stop_stream_query
from util.config import *

def f_merge_df(microdf, batchid):

    # print(f"inside forEachBatch for batchid:{batchid}. Rows in passed dataframe: {microdf.count()}")

    microdf = microdf.dropDuplicates(['ts', 'userId'])

    if (DeltaTable.isDeltaTable(spark, silver_data_path) == False):

        microdf \
        .write \
        .partitionBy(["event_year", "event_month", "event_day"]) \
        .format("delta") \
        .mode("overwrite") \
        .save(silver_data_path)

    else:

        deltadf = DeltaTable.forPath(spark, silver_data_path)

        (deltadf.alias("t")
                .merge(
                    microdf.alias("s"),
                    """ s.event_year  = t.event_year
                    and s.event_month = t.event_month
                    and s.event_day   = t.event_day
                    and s.ts = t.ts
                    and s.userId = t.userId
                    """)
                .whenNotMatchedInsertAll()
                .execute()
        )

if __name__ == '__main__':

    schema = StructType([
        StructField("ts",TimestampType(),True),
        StructField("userId",StringType(),True),
        StructField("sessionId",IntegerType(),True),
        StructField("page", StringType(), True),
        StructField("auth", StringType(), True),
        StructField("method", StringType(), True),
        StructField("status", IntegerType(), True),
        StructField("level", StringType(), True),
        StructField("itemInSession", IntegerType(), True),
        StructField("location", StringType(), True),
        StructField("userAgent", StringType(), True),
        StructField("lastName", StringType(), True),
        StructField("firstName", StringType(), True),
        StructField("registration", LongType(), True),
        StructField("gender", StringType(), True),
        StructField("artist", StringType(), True),
        StructField("song", StringType(), True),
        StructField("length", FloatType(), True)
    ])

    spark = SparkSession.builder \
        .appName(__file__) \
        .config("spark.sql.shuffle.partitions", shuffle_partitions) \
        .config("spark.jars.packages", "io.delta:delta-core_2.12:0.8.0") \
        .config("spark.sql.extensions", "io.delta.sql.DeltaSparkSessionExtension") \
        .config("spark.sql.catalog.spark_catalog", "org.apache.spark.sql.delta.catalog.DeltaCatalog") \
        .getOrCreate()

    spark.sparkContext.setLogLevel("ERROR")

    from delta.tables import *

    # Step 1: Receive data via socket and injest into as Text format.
    # Base on experiment, TEXT consumes less memory

    queryRaw = spark \
            .readStream \
            .format("socket") \
            .option("host", "localhost") \
            .option("port", 3001) \
            .load() \
            .writeStream \
            .queryName("queryRaw") \
            .outputMode("append") \
            .format("text") \
            .option("path", raw_data_path) \
            .option("checkpointLocation", raw_data_ckpt) \
            .trigger(processingTime=trigger_time) \
            .start()

    # Step 2: Convert text into delta format
    df_text = spark \
            .readStream \
            .format("text") \
            .load(raw_data_path)

    df_text = df_text.withColumn("jsonValue", from_json(df_text.value, schema)) \
                 .select(col("jsonValue.ts").alias('ts'),
                         col("jsonValue.userId").alias('userId'),
                         col("jsonValue.sessionId").alias('sessionId'),
                         col("jsonValue.page").alias('page'),
                         col("jsonValue.auth").alias('auth'),
                         col("jsonValue.method").alias('method'),
                         col("jsonValue.status").alias('status'),
                         col("jsonValue.level").alias('level'),
                         col("jsonValue.itemInSession").alias('itemInSession'),
                         col("jsonValue.location").alias('location'),
                         col("jsonValue.userAgent").alias('userAgent'),
                         col("jsonValue.lastName").alias('lastName'),
                         col("jsonValue.firstName").alias('firstName'),
                         col("jsonValue.registration").alias('registration'),
                         col("jsonValue.gender").alias('gender'),
                         col("jsonValue.artist").alias('artist'),
                         col("jsonValue.song").alias('song'),
                         col("jsonValue.length").alias('length')
                         )

    df_text = add_date_field(remove_invalid_record(df_text))

    queryBronze = df_text \
            .writeStream \
            .queryName("queryBronze") \
            .partitionBy(["event_year", "event_month", "event_day"]) \
            .format("delta") \
            .outputMode("append") \
            .option("checkpointLocation", bronze_data_ckpt) \
            .option("path", bronze_data_path) \
            .trigger(processingTime=trigger_time) \
            .start()

    ReadyForNext = False
    while(not ReadyForNext):

        if (os.path.exists(bronze_data_path)
            and
            DeltaTable.isDeltaTable(spark, bronze_data_path)):
            ReadyForNext = True
        else:
            time.sleep(10)

    querySilver = spark \
            .readStream \
            .format("delta") \
            .load(bronze_data_path) \
            .writeStream \
            .queryName("querySilver") \
            .outputMode("append") \
            .format("delta") \
            .foreachBatch(f_merge_df) \
            .option("checkpointLocation", silver_data_ckpt) \
            .trigger(processingTime=trigger_time) \
            .start()

    while spark.streams.active != []:

        for stream_query in spark.streams.active:
            stop_stream_query(stream_query)

        time.sleep(trigger_seconds)

    exit(0)


import pandas as pd
import matplotlib.pyplot as plt

data=[100,200,120,140,160,210,214]

s=pd.Series(data,index=range(len(data)))

s.plot()
plt.title("I am awesome")
plt.xlabel("No idea")
plt.ylabel("More no ide")
plt.show()


import numpy as np  # linear algebra
import pandas as pd  # data processing, CSV file I/O (e.g. pd.read_csv)
from pydantic import BaseModel
import pickle
import catboost as cb

from sklearn.preprocessing import QuantileTransformer

def map_2_cols(df, col1, col2, group, gby):
    return df.loc[:, [col1, col2]].astype(str).sum(axis=1).map(group[gby])

def group_2_cols(df,col1,col2,gby,func='mean'):
    group = df[[col1, col2,gby]].groupby([col1, col2],as_index=False).agg('mean')
    group['comp'] = group[[col1,col2]].astype(str).sum(axis=1)
    group.set_index('comp',inplace = True)
    group.drop([col1, col2],axis=1,inplace = True)
    return group

def cyclical(df, column, max_value):
    """
    The function is encoding time series cyclical features with sin and cos.
    Input:
    ---------
    df - pandas DataFrame
    column - column name
    max_value - column max value
    Output:
    -----------
    -same dataframe with _sin and _cos columns added
    """
    print('>>>>>>>>>>>>>>>>>>>>>', column, "\tmax: ", max_value, '\tdf: ',df[column].values)
    print(df.shape)
    df[column + '_sin'] = np.sin(2 * np.pi * df[column] / max_value)
    df[column + '_cos'] = np.cos(2 * np.pi * df[column] / max_value)
    df.fillna(0, inplace=True)
    return df

def shift_col(df, col_name, shift_num=1):
    df[f"{col_name}_shifted{shift_num}"]= df[col_name] - df[col_name].shift(shift_num)
    df[f"{col_name}_shifted{shift_num}"].fillna(method='bfill',inplace=True)
    df[f"{col_name}_shifted{shift_num}**"]= df[f"{col_name}_shifted{shift_num}"] * df[f"{col_name}_shifted{shift_num}"]
    df[f"{col_name}_shifted{shift_num}**"].fillna(method='bfill',inplace=True)
    df[f"{col_name}_shifted{shift_num}**"].fillna(method='ffill',inplace=True)
    df[f"{col_name}_shifted{shift_num}**"].fillna(method='ffill',inplace=True)
    df.fillna(0, inplace=True)
    return df

def preprocess(df, seasons_Hour_3cut=None, seaons_mean=None, test_set=False):
    df['Holiday'].replace({"Holiday": 0, "No Holiday": 1}, inplace=True)
    df['Functioning Day'].replace({"Yes": 0, "No": 1}, inplace=True)
    df['Seasons'].replace({"Autumn": 2, "Spring": 3, "Summer": 1, "Winter": 4}, inplace=True)
    df['Date'] = pd.to_datetime(df['Date'])
    df['year'] = df['Date'].apply(lambda x:1 if x.year == 2018 else 0)
    df['Hour_3cut'] = pd.cut(df['Hour'],
                       bins=[-np.inf,7,18,np.inf],
                       labels=[1,2,3]).astype('int')
    dummies = pd.get_dummies(df, columns=['Hour', 'Seasons'], prefix=['col1', 'col2'])
#     print(dummies)

    # split the fractions
    df['snow_frac'] = df['Snowfall (cm)'].apply(lambda x: x - int(x))
    df['rain_frac'] = df['Rainfall(mm)'].apply(lambda x: x - int(x))
    df['solar_frac'] = df['Solar Radiation (MJ/m2)'].apply(lambda x: x - int(x))
    df['wind_frac'] = df['Wind speed (m/s)'].apply(lambda x: x - int(x))
#     df['wind_log'] = df['Wind speed (m/s)'].apply(lambda x: np.log(x+1))

    # create dates columns
    df['month'] = df['Date'].apply(lambda x:x.month)
    df['Week Days'] = df['Date'].apply(lambda x:x.dayofweek+1)

    # shift columns -1
    df = shift_col(df, 'Temperature(�C)', -1)
    df = shift_col(df, 'Rainfall(mm)', -1)
    df = shift_col(df, 'Humidity(%)', -1)
    df = shift_col(df, 'Wind speed (m/s)', -1)

    # shift columns
    df = shift_col(df, 'Temperature(�C)')
    df = shift_col(df, 'Rainfall(mm)')
    df = shift_col(df, 'Humidity(%)')
    df = shift_col(df, 'Wind speed (m/s)')

    df = cyclical(df, "month", 12)
    df = cyclical(df, "Hour", 23)
#     df = pd.concat([df, dummies], axis=1)
#     print(type(df))

    return df.drop(["ID", 'Date', 'Temperature(�C)_shifted-1', 'Temperature(�C)_shifted1', 'Functioning Day'], axis=1)

def pipeline(df, model, qt):
    df['Date'] = pd.to_datetime(df['Date'], dayfirst=True).dt.date
    test_X = preprocess(df, test_set=True)
    test_X = pd.DataFrame(qt.transform(test_X))
    y_cbm = model.predict(test_X)
    y_hat = np.exp(y_cbm) - 1
    return y_hat

def predict_file(test_path: str):
    df = pd.read_csv(test_path)
    columns = np.load('bin/columns.npy')
    df.columns = columns
    print('################################', df.info())
    model = cb.CatBoostRegressor()
    model.load_model('bin/cb_model', 'cbm')
    qt = pickle.load(open('bin/qt.pkl', 'rb'))
    predictions = pipeline(df, model, qt)
    ret = pd.DataFrame({'ID': df['ID'].to_numpy(), 'y': predictions}).to_numpy()
    return ret

def predict_single(p_id, date, hour, temperature, humidity, wind_speed, visibility, dew_point, solar_rad, rain_fall,
                   snow_fall, season, holiday, functioning_day):
    columns = np.load('bin/columns.npy')
    df = pd.DataFrame(columns=columns)

    df = df.append({columns[0]: p_id, columns[1]: date, columns[2]: hour, columns[3]: temperature, columns[4]: humidity,
                   columns[5]: wind_speed, columns[6]: visibility, columns[7]: dew_point, columns[8]: solar_rad,
                   columns[9]: rain_fall, columns[10]: snow_fall, columns[11]: season, columns[12]: holiday,
                   columns[13]: functioning_day}, ignore_index=True)
    df['Hour'] = df['Hour'].astype('int')
    model = cb.CatBoostRegressor()
    model.load_model('bin/cb_model', 'cbm')
    qt = pickle.load(open('bin/qt.pkl', 'rb'))
    predictions = pipeline(df, model, qt)
    ret = pd.DataFrame({'ID': df['ID'].to_numpy(), 'y': predictions}).to_numpy()
    return ret

class PredictionItem(BaseModel):
    p_id: str
    date: str
    hour: int
    temperature: float
    humidity: float
    wind_speed: float
    visibility: int
    dew_point: float
    solar_rad: float
    rain_fall: float
    snow_fall: float
    season: str
    holiday: str
    functioning_day: str