'''
This routine to uses the Levenburg-Marquardt algorithm to fit a set of data
points to the function A*np.exp(-((gamma*t)**3)/6) + y0, which describes the
amplitude of a Hahn echo in the presence of a magnetic field gradient and
diffusion.  From the fit, one can determine the constant of self diffusion.
The Hahn echo amplitudes are obtained from an array pasted into the program.
Formally called NMR_diffusion_fit_sec

Last Update 9/29/2012, 10/7/2012 at 12:31 pm by Tycho Sleator
Amended by Patrick Anker & Kaitlyn Morrell (02/28/2018)
'''
import numpy as np
import matplotlib.pyplot as plt
import scipy.optimize
# PK: Removed old imports

# Read the data from the .csv file "data.csv"
#s1 = mlab.csv2rec("Diffusion_data.csv", skiprows=2)

# print out the data names (and type) read in from csv file
#print(s1.dtype)

# assign the data to variables with shorter names
#t = s1.time_ms
#s = s1.echo_size
#sigs = s1.error
print("\n\n")
print("---------------------------------------------------------------------------")
print("File NMR_diffusion_fit_2.py")
print("This routine to uses the Levenburg-Marquardt algorithm to fit a set of data")
print("points to the function A*np.exp(-((gamma*t)**3)/6) + y0, which describes the")
print("amplitude of a Hahn echo in the presence of a magnetic field gradient and")
print("diffusion.  From the fit, one can determine the constant of self diffusion.")
print("---------------------------------------------------------------------------")
# PK: Converted python2 syntax to futureproof python3 syntax

t = 0.001 * np.array([10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120, 130, 140])
s =  np.array([0.74373218544388053, 0.54829865107798392, 0.82055123499403715, 1.0, 0.94426665226231288, 0.92975789160515865, 0.90893941276699119, 0.92556506362147972, 0.91820947988573054, 0.85451130141362652, 0.8240415919133246, 0.81537443267987475, 0.81178126883721502, 0.7646432792463588])
gamma = 5.59 #gyromagnetic ratio
print("t = ", t)
print("s = ", s)

'''
-- information on nonlinear curve fitting --

nlfit,nlpcov = scipy.optimize.curve_fit(f, xdata, ydata, p0=[a0, b0, ...],
  sigma=None, **kwargs)

* f(xdata, a, b, ...): is the fitting function where xdata is an array of values
   of the independent variable and a, b, ... are the fitting parameters, however
   many there are, listed as separate arguments.  f(xdata, a, b, ...) should
   return the y value of the fitting function.
* xdata: is the array containing the x data.
* ydata: is the array containing the y data.
* p0: is a tuple containing the initial guesses for the fitting parameters. The
  guesses for the fitting parameters are set equal to 1 if they are left
  unspecified.
* sigma: is the array containing the uncertainties in the y data.
* **kwargs: are keyword arguments that can be passed to the fitting routine
  numpy.optimize.leastsq that curve_fit calls. These are usually left unspecified.

'''
# Define nonlinear fitting function.
def f(t, A, alpha, y0):
    return A*np.exp(-((gamma**2)*alpha*(t**3))/12) + y0

# Initial guesses: modify these if fit doesn't converge.
A0 = 1.0
alpha0 = 10**(3)
y00 = 0.0

# This is the function that does the nonlinear fit:
#nlfit, nlpcov = scipy.optimize.curve_fit(f, t, s, p0=[A0, gamma0, y00], sigma=None)
nlfit, nlpcov = scipy.optimize.curve_fit(f, t, s, p0=[A0, alpha0, y00], sigma=None)

# These are the parameter estimates (assigned to more readible names):
A_nlfit = nlfit[0]
alpha_nlfit = nlfit[1]
y0_nlfit = nlfit[2]
print('A = {}'.format( A_nlfit))
print('alpha = {}'.format( alpha_nlfit))
print('y0 = {}'.format( y0_nlfit))

'''
Below are the uncertainties in the estimates:  (note that "nlpcov" is the
"covariance matrix".  the diagonal elements of the covariance matrix (nlpcov[i][i])
are the variances of the fit parameters, whose square roots give the standard
deviation or uncertainties in the fit parameters.  The off-diagonal elements give
the correlations between fit parameters.
'''
Asig_nlfit =  np.sqrt(nlpcov[0][0])
alphasig_nlfit = np.sqrt(nlpcov[1][1])
y0sig_nlfit = np.sqrt(nlpcov[2][2])

'''
The following statements generate formatted output of the fit parameters.
The number before the colon refers to the sequence of arguments in the .format
command.  In "{0:6.3f}", the 0 refers to the first argument in the .format()
command, the 6 refers to the total width of the field, and the 3 refers to the
number of digits after the decimal point.
See http://docs.python.org/tutorial/inputoutput.html for more information.
'''

# Compute reduced chi square for nonlinear fit.
#redchisqrNL = (((s - f(t, A_nlfit, alpha_nlfit, y0_nlfit))/sigs)**2).sum()/float(len(t)-2)

# Create an array of times t for the purpose of plotting the fit:
tfit = np.arange(0.0, t[-1] + (t[1]-t[0])/2, 0.0001)

# Generate y-values from parameters determined from the curve fit, so that
# we can plot the fit on top of the data:
sfit = f(tfit, A_nlfit, alpha_nlfit, y0_nlfit)

# Create the figure:
plt.figure(1, figsize = (8,4.5) )

# Draw x and y axes:
plt.axhline(color ='k')
plt.axvline(color ='k')

# Plot the points:
l1 = plt.plot(t, s, 'ob', label='Data')
# Include error bars:
#plt.errorbar(t, s, sigs, fmt='bo')

# Plot the fit:
l2 = plt.plot(tfit,sfit,'r', label='Fit')

# Plot the legend:
plt.legend(loc='upper right' )

# Plot some text with model equation and values of fit parameters.
# Use Latex equation style - $ ... $:
plt.text(0.010, 0.4,
    '$y \, =\, A e^{-(\\gamma^2\\alpha t^3)/12} + y_0$',
    ha='left', va='bottom', size='large')
plt.text(0.010, 0.3,
    '$\\alpha \, =\, ({0:6.4g} \pm {1:5.2g})/s^3$'.format(alpha_nlfit, alphasig_nlfit),
    ha='left', va='bottom', size='large')
plt.text(0.010, 0.2,
    '$A  \, =\, {0:6.3g} \pm  {1:6.1g}$'.format(A_nlfit, Asig_nlfit),
    ha='left', va='bottom', size='large')
plt.text(0.010, 0.1,
    '$y_0 \, =\, {0:6.3g} \pm {1:6.1g}$'.format(y0_nlfit, y0sig_nlfit),
    ha='left', va='bottom', size='large')

# Label the axes:
plt.xlabel('Time (s)',fontsize=14)
plt.ylabel('Echo Amplitude',fontsize=14)

# Specify the plot limits:
#plt.xlim(0.0,max(t))
plt.xlim(0.0,t[-1] + (t[1]-t[0])/2)
plt.ylim(-0.05,1.05)

# Display the figure:
plt.show()


"""
prob : average from data stream
idea : math_prob
comp : O(n)
"""

def curr_avg(arr):
    res, n = 0, len(arr)
    for i in range(n):
        res = (res * i + arr[i]) / (i + 1)
        print(res)

arr = [10, 20, 30, 40, 50, 60]
curr_avg(arr)


# Importing the libraries
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd

df1=pd.read_csv('movies.csv')
df2=pd.read_csv('ratings.csv')

data=pd.merge(df1,df2,on='movieId')
data=data.drop(['genres','timestamp'],axis=1)
data.describe()

data.loc[100836]=[1]+["Toy Story (1995)"]+[611]+[5]
data.loc[100837]=[318]+["Shawshank Redemption, The (1994)"]+[611]+[5]
data.loc[100838]=[47099]+["Pursuit of Happyness, The (2006)"]+[611]+[5]
data.loc[100839]=[3578]+["Gladiator (2000)"]+[611]+[3.5]
data.loc[100840]=[73881]+["3 Idiots (2009)"]+[611]+[5]
data.loc[100841]=[103042]+["Man of Steel (2013)"]+[611]+[3]
data.loc[100842]=[2411]+["Rocky IV (1985)"]+[611]+[4]
data.loc[100843]=[88140]+["Captain America: The First Avenger (2011)"]+[611]+[4]
data.loc[100844]=[103688]+["Conjuring, The (2013)"]+[611]+[4.5]
data.loc[100845]=[106782]+["Wolf of Wall Street, The (2013)"]+[611]+[5]

ratings = pd.DataFrame(data.groupby('title')['rating'].mean()).reset_index()
ratings.head()
temp = pd.DataFrame(data.groupby('title')['rating'].count()).reset_index()
temp=temp.drop(['title'],axis=1)
ratings['number_of_ratings']=temp
ratings.head()

Y = data.pivot_table(index=['userId'], columns=['title'], values='rating',fill_value=0)

columns=list(Y.columns)

Y=Y.to_numpy()

R = data.pivot_table(index='userId', columns='title', values='rating',fill_value=0)
R[R != 0] = 1

R=R.to_numpy()

Y=Y.T
R=R.T

nm=9719
nu=612

X=np.random.randn(nm,100)*0.01
Theta=np.random.randn(nu,100)*0.01

def cost_function(X,Theta,Y,R):
    Xtheta=(np.dot(X,Theta.T))*R

    Xc=Xtheta[Xtheta!=0]
    Yc=Y[Y!=0]
    cost=round(0.5*(np.power((Xc-Yc),2).sum()),2)
    return cost

def compute_gradients(X,Theta,Y,R):
    dX=np.zeros(X.shape)
    dTheta=np.zeros(Theta.shape)
    dX=np.dot(R*(np.dot(X,Theta.T)-Y),Theta)
    dTheta=np.dot((R*(np.dot(X,Theta.T)-Y)).T,X)
    return dX,dTheta

def gradient_descent(X,Theta,Y,R,iterations,alpha):
    for i in range(iterations):

        cost=cost_function(X,Theta,Y,R)
        dX,dTheta=compute_gradients(X,Theta,Y,R)
        X=X-(alpha*dX)
        Theta=Theta-(alpha*dTheta)
        print(cost)
    return X,Theta

Xtrained,Thetatrained=gradient_descent(X,Theta,Y,R,5000,6*1e-4)

import pickle
pickle.dump([Xtrained,Thetatrained], open("trial.p", "wb"))


__author__ = 'Marcus'

from cassandra.cluster import Cluster
from controller import DBmanager
from controller.Config import Config

export_to_csv_query = None

# Export to CUAHSI file
def export_to_csv(session, tablename, attributes, path):

    global export_to_csv_query

    if export_to_csv_query is None:

        attr = []
        for val in attributes[:-1]:
            attr.append(val + ",")
        attr.append(attributes[-1])

        string_attributes = "".join(attr)

        export_to_csv_query = ("""
            COPY %s (%s) TO '%s'
        """ % (tablename, string_attributes, path))

        print(export_to_csv_query)

        session.execute(export_to_csv_query)

    export_to_csv_query = None

if __name__ == '__main__':

    att = ['datavalue',
            'valueaccuracy',
            'localdatetime',
            'utcoffset',
            'datetimeutc',
            'sitecode',
            'variablecode',
            'offsetvalue',
            'offsettypecode',
            'censorcode',
            'qualifiercode',
            'methodcode',
            'sourcecode',
            'samplecode',
            'derivedfromid',
            'qualitycontrollevelcode']

    config = Config()
    params = config.read_config("erken")
    db = DBmanager.DBManager(params)
    export_to_csv(db.session,"island_cuahsi",att,'temp.csv')


def gcd1(m1,n1):
    rem=m1%n1
    if(rem==0):
        return n1
    else:
        return gcd1(n1,rem)
def gcd(m,n,r):
    g=gcd1(m,n)
    h=gcd1(g,r)
    return h
flag=0
s=int(input())
l=[]
while(s>3):
    for i in range(1,s):
        for j in range(i,s-1):
            for k in range(i,s-2):
                if(i+j+k==s and gcd(i,j,k)==1):
                    l.append(i)
                    l.append(j)
                    l.append(k)
                    flag=1
                    break
            if flag==1:
                break
        if(flag==1):
            break
    if(flag==1):
            break
    else:
        break
l=[str(i) for i in l]
l.sort()
print(' '.join(l) if len(l)>0 else "0")


import multiprocessing
from pathlib import Path

if Path('/vagrant').exists():
    chdir = '/vagrant'
else:
    chdir = str(Path.home())

workers = max(multiprocessing.cpu_count(), 2)

bind = '0.0.0.0:80'
keepalive = 120
errorlog = 'gunicorn.log'
pidfile = 'gunicorn.pid'
timeout = 5

worker_class = 'aiohttp.worker.GunicornUVLoopWebWorker'


# SSD1351_16bit.py MicroPython driver for Adafruit color OLED displays.

# Adafruit 1.5" 128*128 OLED display: https://www.adafruit.com/product/1431
# Adafruit 1.27" 128*96 display https://www.adafruit.com/product/1673
# For wiring details see drivers/ADAFRUIT.md in this repo.

# This driver is based on the Adafruit C++ library for Arduino
# https://github.com/adafruit/Adafruit-SSD1351-library.git

# Copyright (c) Peter Hinch 2019-2020
# Released under the MIT license see LICENSE

import framebuf
import utime
import gc
import micropython
from drivers.boolpalette import BoolPalette

# https://github.com/peterhinch/micropython-nano-gui/issues/2
# The ESP32 does not work reliably in SPI mode 1,1. Waveforms look correct.
# Now using 0,0 on STM and ESP32

# Initialisation commands in cmd_init:
# 0xfd, 0x12, 0xfd, 0xb1,  # Unlock command mode
# 0xae,  # display off (sleep mode)
# 0xb3, 0xf1,  # clock div
# 0xca, 0x7f,  # mux ratio
# 0xa0, 0x74,  # setremap 0x74
# 0x15, 0, 0x7f,  # setcolumn
# 0x75, 0, 0x7f,  # setrow
# 0xa1, 0,  # set display start line
# 0xa2, 0,  # displayoffset
# 0xb5, 0,  # setgpio
# 0xab, 1,  # functionselect: serial interface, internal Vdd regulator
# 0xb1, 0x32,  # Precharge
# 0xbe, 0x05,  # vcommh
# 0xa6,  # normaldisplay
# 0xc1, 0xc8, 0x80, 0xc8,  # contrast abc
# 0xc7, 0x0f,  # Master contrast
# 0xb4, 0xa0, 0xb5, 0x55,  # set vsl (see datasheet re ext circuit)
# 0xb6, 1,  # Precharge 2
# 0xaf,  # Display on

class SSD1351(framebuf.FrameBuffer):
    # Convert r, g, b in range 0-255 to a 16 bit colour value RGB565
    #  acceptable to hardware: rrrrrggggggbbbbb
    @staticmethod
    def rgb(r, g, b):
        return ((r & 0xf8) << 5) | ((g & 0x1c) << 11) | (b & 0xf8) | ((g & 0xe0) >> 5)

    def __init__(self, spi, pincs, pindc, pinrs, height=128, width=128, init_spi=False):
        if height not in (96, 128):
            raise ValueError('Unsupported height {}'.format(height))
        self.spi = spi
        self.spi_init = init_spi
        self.pincs = pincs
        self.pindc = pindc  # 1 = data 0 = cmd
        self.height = height  # Required by Writer class
        self.width = width
        mode = framebuf.RGB565
        self.palette = BoolPalette(mode)
        gc.collect()
        self.buffer = bytearray(self.height * self.width * 2)
        super().__init__(self.buffer, self.width, self.height, mode)
        self.mvb = memoryview(self.buffer)
        pinrs(0)  # Pulse the reset line
        utime.sleep_ms(1)
        pinrs(1)
        utime.sleep_ms(1)
        if self.spi_init:  # A callback was passed
            self.spi_init(spi)  # Bus may be shared
        # See above comment to explain this allocation-saving gibberish.
        self._write(b'\xfd\x12\xfd\xb1\xae\xb3\xf1\xca\x7f\xa0\x74'\
        b'\x15\x00\x7f\x75\x00\x7f\xa1\x00\xa2\x00\xb5\x00\xab\x01'\
        b'\xb1\x32\xbe\x05\xa6\xc1\xc8\x80\xc8\xc7\x0f'\
        b'\xb4\xa0\xb5\x55\xb6\x01\xaf', 0)
        self.show()
        gc.collect()

    def _write(self, mv, dc):
        self.pincs(1)
        self.pindc(dc)
        self.pincs(0)
        self.spi.write(bytes(mv))
        self.pincs(1)

    # Write lines from the framebuf out of order to match the mapping of the
    # SSD1351 RAM to the OLED device.
    def show(self):
        mvb = self.mvb
        bw = self.width * 2  # Width in bytes
        if self.spi_init:  # A callback was passed
            self.spi_init(self.spi)  # Bus may be shared
        self._write(b'\x5c', 0)  # Enable data write
        if self.height == 128:
            for l in range(128):
                l0 = (95 - l) % 128  # 95 94 .. 1 0 127 126 .. 96
                start = l0 * self.width * 2
                self._write(mvb[start : start + bw], 1)  # Send a line
        else:
            for l in range(128):
                if l < 64:
                    start = (63 -l) * self.width * 2  # 63 62 .. 1 0
                elif l < 96:
                    start = 0
                else:
                    start = (191 - l) * self.width * 2  # 127 126 .. 95
                self._write(mvb[start : start + bw], 1)  # Send a line


# -*- coding: utf-8 -*-
from nltk.tokenize import sent_tokenize
from spacy.en import English
import re
from nltk.tag.stanford import StanfordNERTagger
import nltk as n_ltk

class SentenceSplit(object):
    NLTK_SPLITTER = 1
    SPACY_SPLITTER = 2
    STANFORD_SPLITTER = 3
    DEFAULT_FILTERS = ['^<{0,1}http.{0,1}:',
                       '^.{0,2}\n{2,4}',
                       '^.*@.*',
                       '^_+',
                       '^<mailto:']

    def __init__(self, tpe):

        self.content = None
        self.split_sents = []
        self.type = tpe

    def factory(self, msg):
        """
        @type msg Message.MessageBlock
        """
        if self.type == 1:
            return NltkSplitter(object)
        if self.type == 2:
            return SpacySplitter(object)
        if self.type == 3:
            return StanfordSplitter(object)

class NltkSplitter(SentenceSplit):

    def get_sentences(self, text):
        """
        @type text str
        @:rtype list[str]
        """
        for t in text:
            self.split_sents = sent_tokenize(t)
        return self.split_sents

    @staticmethod
    def filter(fltr_pkg, text):
        """
        @type text str
        @type fltr_pkg list[str]
        @:rtype list[str]
        """
        matched = False
        for reg in fltr_pkg:
            if re.match(reg, text):
                matched = True
                break
        return matched

    def locate_signature(self, tag_list):
        others = 0.00001
        tagged = 0.0
        lst = []
        token_sentence = n_ltk.word_tokenize(tag_list)
        tagged_sent = n_ltk.pos_tag(token_sentence)
        chunks = n_ltk.ne_chunk(tagged_sent)
        for t in chunks:
            if hasattr(t, 'label') and t.label:
                tagged += 1
                label = t.label
                word = t[0][0]
            else:
                others += 1
                label = 'O'
                word = t[0]
            lst.append((word, label))
        lst.append(tagged / others)
        return lst

class SpacySplitter(SentenceSplit):
    print("Loading Spacy English Model...")
    nlp = English()

    def get_sentences(self, text):
        """
        @type text str
        @:rtype list[spacy.tokens.doc.Doc]
        """

        for t in text:
            doc = self.nlp(u'%s' % t)
            for s in doc.sents:
                self.split_sents.append(s)
        return self.split_sents

    @staticmethod
    def filter(fltr_pkg, text):
        """
        @type text spacy.tokens.doc.Doc
        @type fltr_pkg list[str]
        @:rtype list[str]
        """
        matched = False
        for reg in fltr_pkg:
            if re.match(reg, text.text):
                matched = True
                break
        return matched

    @staticmethod
    def locate_signature(tag_list):
        others = 0.00001
        tagged = 0.0
        lst = []
        for token in tag_list:
            if re.match('^\n', token.text):
                continue
            if token.ent_iob_ != "O":
                tpe = token.ent_type_
                tagged += 1
            else:
                tpe = 'O'
                others += 1
            lst.append((token.text, tpe))
        lst.append(tagged / others)
        return lst

class StanfordSplitter(SentenceSplit):
    tagger = StanfordNERTagger(
        '.\stanford\english.all.3class.distsim.crf.ser.gz',
        '.\stanford\stanford-ner.jar')

    def get_sentences(self, text):
        """
        @type text str
        @:rtype list[str]
        """
        for t in text:
            self.split_sents = sent_tokenize(t)
        return self.split_sents

    @staticmethod
    def filter(fltr_pkg, text):
        """
        @type text str
        @type fltr_pkg list[str]
        @:rtype list[str]
        """
        matched = False
        for reg in fltr_pkg:
            if re.match(reg, text):
                matched = True
                break
        return matched

    @staticmethod
    def locate_signature(self, tag_list):
        others = 0.00001
        tagged = 0.0
        lst = []
        tags = self.tagger.tag(tag_list.split())
        for word, iob in tags:
            if re.match('^\n', word):
                continue
            if iob != "O":
                tagged += 1
            else:
                others += 1
            lst.append((word, iob))
        lst.append(tagged / others)
        return lst


import os
from pathlib import Path

# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent

AUTH_USER_MODEL = "users.Account"
LOGIN_URL = '/accounts/login/'
ADMIN_URL = "workbook-factory/admin/"
# LOGIN_REDIRECT_URL = "/app/"

DATE_FORMAT = "d/m/Y"
DATE_INPUT_FORMATS = ('%m/%d/%Y',)

# Application definition

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.admindocs',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.sites',
    'django.contrib.staticfiles',

    'wagtail.contrib.forms',
    'wagtail.contrib.redirects',
    'wagtail.embeds',
    'wagtail.sites',
    'wagtail.users',
    'wagtail.snippets',
    'wagtail.documents',
    'wagtail.images',
    'wagtail.search',
    'wagtail.admin',
    'wagtail.core',

    'modelcluster',
    'taggit',

    'allauth',
    'allauth.account',
    # 'allauth.socialaccount',

    'rest_framework',

    'config',
    # 'cms',
    'users',
    'utils',
]

DEFAULT_AUTO_FIELD = 'django.db.models.AutoField'

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
    'wagtail.contrib.redirects.middleware.RedirectMiddleware',
]

ROOT_URLCONF = 'config.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR, 'templates'), ],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

WSGI_APPLICATION = 'config.wsgi.application'

SITE_ID = 1

# Password validation
# https://docs.djangoproject.com/en/3.1/ref/settings/#auth-password-validators

AUTH_PASSWORD_VALIDATORS = [
    {
        'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
    },
]

# Allauth settings
ACCOUNT_EMAIL_REQUIRED = True
ACCOUNT_EMAIL_VERIFICATION = 'mandatory'
ACCOUNT_AUTHENTICATION_METHOD = "email"
ACCOUNT_SIGNUP_FORM_CLASS = 'users.forms.SignupForm'

# Internationalization
# https://docs.djangoproject.com/en/3.1/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True

# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/3.1/howto/static-files/

STATICFILES_FINDERS = [
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
]

STATICFILES_DIRS = [
    os.path.join(BASE_DIR, os.pardir, 'static'),
]

# STATIC_ROOT = '/Users/dave/sandbox/overwatch-api/static/'
STATIC_URL = '/static/'

MEDIA_ROOT = os.path.join(BASE_DIR, os.pardir, 'uploads')
MEDIA_URL = '/uploads/'

FILE_UPLOAD_PERMISSIONS = 0o644

WAGTAIL_SITE_NAME = 'Workbook Factory'
# WAGTAILIMAGES_IMAGE_MODEL = 'cms.CustomImage'
# PASSWORD_REQUIRED_TEMPLATE = 'account/login.html'
WAGTAIL_FRONTEND_LOGIN_URL = '/accounts/login/'


# -*- coding: utf-8 -*-
"""
    flask.ext.fillin.wrapper
    ~~~~~~~~~~~~~~~~~~~~~~~~

    Add modifiy response class with form parsing support.

    :copyright: (c) 2012 by Christoph Heer.
    :license: BSD, see LICENSE for more details.
"""

import types

from flask.wrappers import Response
from lxml.html import document_fromstring

class FormWrapper(Response):
    """An additional wrapper for the :class:`~flask.testing.FlaskClient` which
    adds :data:`form` and :data:`forms` as parameter of the response.::

        from flask.ext.fillin import FormWrapper

        client = FlaskClient(flask_app, response_wrapper=FormWrapper)
        response = client.get('/page_with_form')

        response.form.fields['username'] = 'my username'
        response.form.fields['password'] = 'secret'
        response.form.fields['remember'] = True

        response.form.submit(client)

        Adds also :data:`html` which is parsed lxml HtmlElement and methods
        :data:`link` and :data:`links` for link access.

        Each link has method click(client) which calls client.get(href).
    """

    _parsed_html = None

    @property
    def html(self):
        if self._parsed_html is None:
            self._parsed_html = document_fromstring(self.data)

            # add click function to all links
            def _click(self, client, **kwargs):
                path = self.attrib['href']
                return client.get(path, **kwargs)

            for link in self._parsed_html.cssselect('a'):
                setattr(link, 'click', types.MethodType(_click, link))

            # add submit function to all links
            def _submit(self, client, path=None, submit_name=None, **kargs):
                data = { k: '' if self._should_be_blank(k, v) else v
                               for k, v in self.fields.iteritems() }

                # validate and set values from files
                for key, value in self.files.iteritems():
                    if key not in self.inputs:
                        raise ValueError("No input of with name %s" % repr(key))
                    if self.inputs[key].type != "file":
                        raise ValueError("Input %s is not of type 'file'" % repr(key))
                    if not isinstance(value, file):
                        raise TypeError("Set file input %s with non-file %s" % (repr(key), repr(value)))
                    data[key] = value

                if kargs.has_key('data'):
                    data.update(kargs['data'])
                    del kargs['data']
                if path is None:
                    path = self.action
                if not kargs.has_key('method'):
                    kargs['method'] = self.method
                return client.open(path, data=data, **kargs)

            def _should_be_blank(self, k, v):
                # note that most browsers submit '' instead of None for empty fields
                blank_fields = ['text', 'password']

                field = self.inputs[k]
                if hasattr(field, 'type'):
                  return field.type in blank_fields and v is None
                else:
                  return False

            for form in self._parsed_html.forms:
                setattr(form, "files", {})  # TODO, validate that input is a file handle upon assigment
                setattr(form, "submit", types.MethodType(_submit, form))
                setattr(form, "_should_be_blank", types.MethodType(_should_be_blank, form))

        return self._parsed_html

    @property
    def forms(self):
        """A list of all received forms in the same way like
        `lxml <http://lxml.de/lxmlhtml.html#forms>`_ with the same functions
        and an additional function to submit the form over a test client.
        """
        return self.html.forms

    @property
    def form(self):
        """The first received form from the page."""
        return self.forms[0]

    def links(self, css_expression="a"):
        """Get all the links by css_express"""
        return self.html.cssselect(css_expression)

    def link(self, css_expression="a"):
        """Get first link by css_expression"""
        links = self.links(css_expression)
        if links:
            return links[0]


import re

triangle = []

with open('triangle.txt') as file:
    for line in file.readlines():
        new_line = []
        for number in line.split():
            if re.match('0\d+', number):
                new_line.append(int(number[1:]))
            else:
                new_line.append(int(number))
        triangle.append(new_line)

def max_sum():
    for i in range(len(triangle) - 2, -1, -1):
        for j in range(len(triangle[i])):
            triangle[i][j] += max(triangle[i + 1][j], triangle[i + 1][j + 1])
    return triangle[0][0]

print(max_sum())


# Copyright (C) 2017 Google Inc.
# Licensed under http://www.apache.org/licenses/LICENSE-2.0 <see LICENSE file>

"""
  Audit permission hooks

  The following permission hooks make sure archived audits are not editable
"""
import itertools

from werkzeug.exceptions import Forbidden

from ggrc import db
from ggrc.models import all_models
from ggrc.services import signals

_AUDIT_MODEL_NAME = 'Audit'

@signals.Restful.collection_posted.connect_via(all_models.Audit)
def handle_audit_post(sender, objects=None, sources=None):
  """Handles creating issue tracker related info."""
  del sender  # Unused
  for obj, src in itertools.izip(objects, sources):
    issue_tracker_info = src.get('issue_tracker')
    if not issue_tracker_info:
      continue
    all_models.IssuetrackerIssue.create_or_update_from_dict(
        _AUDIT_MODEL_NAME, obj.id, issue_tracker_info)

@signals.Restful.model_put.connect_via(all_models.Audit)
def handle_audit_put(sender, obj=None, src=None, service=None):
  """Handles updating issue tracker related info."""
  del sender, service  # Unused
  issue_tracker_info = src.get('issue_tracker')
  if issue_tracker_info:
    all_models.IssuetrackerIssue.create_or_update_from_dict(
        _AUDIT_MODEL_NAME, obj.id, issue_tracker_info)

@signals.Restful.model_deleted_after_commit.connect_via(all_models.Audit)
def handle_audit_deleted_after_commit(sender, obj=None, service=None,
                                      event=None):
  """Handles deleting issue tracker related info."""
  del sender, service, event  # Unused
  issue_obj = all_models.IssuetrackerIssue.get_issue(
      _AUDIT_MODEL_NAME, obj.id)
  if issue_obj:
    db.session.delete(issue_obj)

def init_hook():
  """Initializes audit permission hooks."""
  # pylint: disable=unused-variable
  @signals.Restful.model_put.connect_via(all_models.Audit)
  @signals.Restful.model_deleted.connect_via(all_models.Audit)
  def handle_audit_permission_put(sender, obj, src=None, service=None):
    """Make sure admins cannot delete/update archived audits"""
    # pylint: disable=unused-argument
    if obj.archived and not db.inspect(
       obj).get_history('archived', False).has_changes():
      raise Forbidden()

  # pylint: disable=unused-variable
  @signals.Restful.model_deleted.connect_via(all_models.Assessment)
  @signals.Restful.model_deleted.connect_via(all_models.AssessmentTemplate)
  @signals.Restful.model_posted.connect_via(all_models.Assessment)
  @signals.Restful.model_posted.connect_via(all_models.AssessmentTemplate)
  @signals.Restful.model_put.connect_via(all_models.Assessment)
  @signals.Restful.model_put.connect_via(all_models.AssessmentTemplate)
  @signals.Restful.model_put.connect_via(all_models.Snapshot)
  def handle_archived_object(sender, obj=None, src=None, service=None):
    """Make sure admins cannot delete/update archived audits"""
    # pylint: disable=unused-argument
    if obj.archived:
      raise Forbidden()

  @signals.Restful.model_deleted.connect_via(all_models.Comment)
  @signals.Restful.model_deleted.connect_via(all_models.Document)
  @signals.Restful.model_deleted.connect_via(all_models.UserRole)
  @signals.Restful.model_posted.connect_via(all_models.Comment)
  @signals.Restful.model_posted.connect_via(all_models.Document)
  @signals.Restful.model_posted.connect_via(all_models.Snapshot)
  @signals.Restful.model_posted.connect_via(all_models.UserRole)
  def handle_archived_context(sender, obj=None, src=None, service=None):
    """Make sure admins cannot delete/update archived audits"""
    # pylint: disable=unused-argument
    if (hasattr(obj, 'context') and
        hasattr(obj.context, 'related_object') and getattr(
            obj.context.related_object, 'archived', False)):
      raise Forbidden()

  @signals.Restful.model_posted.connect_via(all_models.Relationship)
  @signals.Restful.model_deleted.connect_via(all_models.Relationship)
  def handle_archived_relationships(sender, obj=None, src=None, service=None):
    """Make sure users can not map objects to archived audits"""
    # pylint: disable=unused-argument
    if (getattr(obj, 'source_type', None) == 'Issue' or
       getattr(obj, 'destination_type', None) == 'Issue'):
      # Issues can be mapped even if audit is archived so skip the permission
      # check here
      return
    if (hasattr(obj, 'context') and
        hasattr(obj.context, 'related_object') and getattr(
            obj.context.related_object, 'archived', False)):
      raise Forbidden()


#!flask/bin/python
from flask import Flask, jsonify
from flask import abort
from flask import make_response
from flask import request
from flask import url_for
from flask.ext.httpauth import HTTPBasicAuth

app = Flask(__name__)

auth = HTTPBasicAuth()

@auth.get_password
def get_password(username):
    if username == 'prasanth':
        return 'password'
    return None

@auth.error_handler
def unauthorized():
    return make_response(jsonify({'error': 'Please Enter Your Username and Password'}), 401)

reminders = [
    {
        'id': 1,
        'title': u'Market',
        'desc': u'Chips and Dips',
        'finish': False
    },
    {
        'id': 2,
        'title': u'Interview at 3 p.m',
        'desc': u'Get shirt ironed',
        'finish': False
    },
     {
        'id': 3,
        'title': u'Meeting with the Boss',
        'desc': u'Bring the report',
        'finish': False
    }

]

@app.route('/todo/api/v1/todoabc/<int:todoabc_id>', methods=['DELETE'])
def delete_todoabc(todoabc_id):
    todoabc = [todoabc for todoabc in reminders if todoabc['id'] == todoabc_id]
    if len(todoabc) == 0:
        abort(404)
    reminders.remove(todoabc[0])
    return jsonify({'result': True})

@app.route('/todo/api/v1/todoabc/<int:todoabc_id>', methods=['PUT'])
def update_todoabc(todoabc_id):
    todoabc = [todoabc for todoabc in reminders if todoabc['id'] == todoabc_id]
    if len(todoabc) == 0:
        abort(404)
    if not request.json:
        abort(400)
    if 'title' in request.json and type(request.json['title']) != unicode:
        abort(400)
    if 'desc' in request.json and type(request.json['desc']) is not unicode:
        abort(400)
    if 'finish' in request.json and type(request.json['finish']) is not bool:
        abort(400)
    todoabc[0]['title'] = request.json.get('title', todoabc[0]['title'])
    todoabc[0]['desc'] = request.json.get('desc', todoabc[0]['desc'])
    todoabc[0]['finish'] = request.json.get('finish', todoabc[0]['finish'])
    return jsonify({'todoabc': todoabc[0]})

@app.route('/todo/api/v1/todoabc', methods=['POST'])
def create_todoabc():
    if not request.json or not 'title' in request.json:
        abort(400)
    todoabc = {
        'id': reminders[-1]['id'] + 1,
        'title': request.json['title'],
        'desc': request.json.get('desc', ""),
        'finish': False
    }
    reminders.append(todoabc)
    return jsonify({'listofreminders': reminders}), 201

@app.route('/todo/api/v1/todoabc', methods=['GET'])
@auth.login_required
def get_todolists():
    return jsonify({'todolists': [make_public_todoabc(todoabc) for todoabc in reminders]})

def make_public_todoabc(todoabc):
    new_todoabc = {}
    for field in todoabc:
        if field == 'id':
            new_todoabc['uri'] = url_for('get_todoabc', todoabc_id=todoabc['id'], _external=True)
        else:
            new_todoabc[field] = todoabc[field]
    return new_todoabc
@app.errorhandler(404)
def not_found(error):
    return make_response(jsonify({'error': 'Not found'}), 404)

@app.route('/todo/api/v1/todoabc/<int:todoabc_id>', methods=['GET'])
def get_todoabc(todoabc_id):
    todoabc = [todoabc for todoabc in reminders if todoabc['id'] == todoabc_id]
    if len(todoabc) == 0:
        abort(404)
    return jsonify({'todoabc': todoabc[0]})

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


import math, time

class Player:
    def __init__(self, Screen):
        self.gameScreen = Screen                    # stores Game Screen
        self.pygame = Screen.pygame                 # pygame library
        self.screen = Screen.screen                 # stores pygame.screen

        #self.playerX = 300
        #self.playerY = 300

        # creates a rectangle which is 'underneath' the object
        # the attributes X and Y can be get by: rect.x and rect.y
        self.rect = self.pygame.Rect(300, 300, 50, 30)

        self.angle = 0                              # start-angle (pointing towards right-side of screen)
        self.angleStep = 4                          # angle-step variation at key-press
        self.playerSpeed = 0                        # player current speed
        self.platerMaxSpeed = 5.5                   # player's max speed
        self.playerAcceleration = 0.1               # speed-step variation at ket-press
        self.waterDragConstant = 0.07               # defines water drag force
        self.playerImage = self.pygame.transform.scale( (self.pygame.image.load("archive/boat.png")), (50, 30) )
        self.originalImage = self.playerImage       # stores original image which this only will be rotated due to memory bug
        self.update_player()                        # updates player

        self.score = 0

    def update_score(self, score):
        ''' Will increase when collects collectible objects, and decrease when hits animais and/or plants. '''
        print(score)
        self.score += score
        print(score)

    def get_score(self):
        return self.score

    def accelerate_player(self, way):
        ''' Increases (UPKEY) or Decreases (DOWNKEY) boat's current speed. '''
        if way == "ahead":
            # hits gas pedal if not at max speed
            if self.playerSpeed <= self.platerMaxSpeed:
                self.playerSpeed += self.playerAcceleration
        if way == "reverse":
            # releases gas pedal and reverses when speed<0, if not at maximum reverse speed
            if self.playerSpeed >= ((self.platerMaxSpeed - 0.5)*(-1)):
                self.playerSpeed -= self.playerAcceleration
        self.update_player()

    def break_player(self):
        self.playerSpeed = 0

        ''' Gradually slows down boat until fully stops. '''
        '''if self.playerSpeed > 0:
            while self.playerSpeed > 0:
                self.playerSpeed -= self.waterDragConstant
                self.calculate_new_xy(self.playerSpeed)
                self.update_player()
                time.sleep(0.05)
        elif self.playerSpeed < 0:
            while self.playerSpeed < 0:
                self.playerSpeed += self.waterDragConstant
                self.calculate_new_xy(self.playerSpeed)
                self.update_player()
                time.sleep(0.05)
        self.playerSpeed = 0'''

    def move_player(self):
        ''' Moves boat when there is acceleration (speed>0). '''
        self.calculate_new_xy(self.playerSpeed)
        self.update_player()

    def rotate_player(self, direction):
        ''' Rotates boat when RIGHTKEY or LEFTKEY are pressed. '''
        if direction == "positive":
            self.angle += self.angleStep % 360
            self.playerImage = self.pygame.transform.rotate(self.originalImage, self.angle)
        if direction == "negative":
            self.angle -= self.angleStep % 360
            self.playerImage = self.pygame.transform.rotate(self.originalImage, self.angle)
        self.update_player()

    def calculate_new_xy(self,speed):
        ''' Calculates boat's new location, when moving, based on it's angle direction. '''
        if self.rect.x > 0 and self.rect.x < 750:
            self.rect.x = self.rect.x + (speed*math.cos((-1)*self.angle*math.pi/180))
        else:
            self.angle = ( 180 - self.angle ) % 360 #( self.angle + 180 ) % 360
            self.rect.x = self.rect.x + (speed*math.cos((-1)*self.angle*math.pi/180))
            self.update_player()

        if self.rect.y > 0 and self.rect.y < 570:
            self.rect.y = self.rect.y + (speed*math.sin((-1)*self.angle*math.pi/180)) + 1
        else:
            if self.rect.y <= 0:
                self.angle = ( 360 - self.angle ) % 360 #( self.angle + 180 ) % 360
                self.rect.y = self.rect.y + 1#(speed*math.cos((-1)*self.angle*math.pi/180))
            if self.rect.y >= 570:
                if self.angle > 0 and self.angle < 180:
                    self.rect.y = self.rect.y + (speed*math.sin((-1)*self.angle*math.pi/180)) + 1

    def update_player(self):
        ''' Updates boat's image on screen. '''
        self.gameScreen.update_background()
        self.screen.blit( self.playerImage, (self.rect.x, self.rect.y) )


import cv2
import pickle
import os
import numpy as np
import matplotlib.pyplot as plt
import random
DATADIR = "/home/icefire/ML/CORONA/CoronaCTScan/Data_Images"
CATEGORIES = ['Negative','Positive']
training_data = []
IMG_SIZE = 60
def create_training_data():
    for category in CATEGORIES:
        path = os.path.join(DATADIR,category)
        class_num = CATEGORIES.index(category)
        for img in os.listdir(path):
            try:

                img_array = cv2.imread(os.path.join(path,img),cv2.IMREAD_GRAYSCALE)
                newArray = cv2.resize(img_array,(IMG_SIZE,IMG_SIZE))
                training_data.append([newArray,class_num])
            except Exception as e:
                pass
create_training_data()
random.shuffle(training_data)

X = []
y = []

for features,labels in training_data:
    X.append(features)
    y.append(labels)

X = np.array(X).reshape(-1,IMG_SIZE,IMG_SIZE,1)
pickle_out = open("X.pickle","wb")
pickle.dump(X,pickle_out)
pickle_out.close()
pickle_out = open("y.pickle","wb")
pickle.dump(y,pickle_out)
pickle_out.close()


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

'''Containers including Sequential and Branch.'''

from dango.nodes.base_layer import BaseLayer
from dango.nodes.pooling import Pool2d
import tensorflow as tf

class Sequential(object):
    '''Sequential containers.

    attribute: layers: list
        containing layer instances.
    attribute: collections: list
        collected info of this sequential.
    '''

    _sequential_counter = 1

    def __init__(self, name_scope=None, **kwargs):
        '''
        param: name_scope: str or None, optional
            name scope of this sequential.
        '''
        if name_scope is None:
            name_scope = 'sequential' + str(Sequential._sequential_counter)
            Sequential._sequential_counter += 1
        self.name_scope = name_scope

        self.layers, self.collections = list(), list()
        self.kwargs = kwargs

    def __enter__(self):
        return self

    def __exit__(self, *args):
        for layer in self.layers:
            layer.variable_scope = self.name_scope + '-' + layer.name_scope
            for key, value in self.kwargs.items():
                if hasattr(layer, key): setattr(layer, key, value)

    def add(self, layer):
        '''Add layer to this container.

        param: layer:
            layer instance.
        return: layer
            same layer instance.
        '''
        self.layers.append(layer)
        return layer

    def _logger(self):
        '''Logging of this sequential.'''
        messages = ['[{}]'.format(self.name_scope)]
        for layer_info in self.collections:
            messages.append('--[name_scope]: {}'.format(
                layer_info['name_scope']))
            for key, value in layer_info.items():
                if key != 'name_scope':
                    messages.append('----{}: {}'.format(
                        key, value))
        tf.logging.info('network(s) info'.center(100, '*'))
        [tf.logging.info(msg) for msg in messages]

    def __call__(self, data_flow):
        '''Forward.'''
        with tf.name_scope(self.name_scope):
            for layer in self.layers:
                data_flow = layer(data_flow)

                fail_msg = "output of {}-{} is not finite".format(
                    self.name_scope, layer.name_scope)
                data_flow = tf.verify_tensor_all_finite(
                    data_flow, fail_msg)

                self.collections.append(layer.__dict__)

        self._logger()
        return data_flow

class Branch(Sequential):
    '''Branch containers.

    attribute: pairs: list
        an intermediate list to generate `down_path` and `up_path`.
    attribute: down_path: dict
        key   <--> index of node in down path.
        value <--> some properties will be used in connection.
    attribute: up_path: dict
        key   <--> index of node in up path.
        value <--> index of connection node in down path.
    '''

    _branch_counter = 1
    _layer_types = ['down', 'up']
    _connection = ['add', 'concat']

    def __init__(self, name_scope=None, **kwargs):
        if name_scope is None:
            name_scope = 'branch' + str(Branch._branch_counter)
            Branch._branch_counter += 1
        super(Branch, self).__init__(name_scope=name_scope, **kwargs)

        self.pairs = list()
        self.down_path, self.up_path = dict(), dict()

    def add(self, layer,
                  layer_type=None,
                  decrease_scale=1,
                  connection='concat',
                  another_task_flag=False):
        '''Add layer to this container.

        NOTE: how to use `down` and `up` as `layer_type`?
        1) the 'layer' which will generate data that have two branches should
        be equipped with 'down' layer-type;
        2) the 'layer' which will generate data that will be concated by other
        data should be equipped with 'up' layer-type.

        param: layer:
            layer instance.
        param: layer_type: str or None, optional
            should be one of Branch._layer_types or None.
        param: decrease_scale: int, optional
            scale for decreasing size, default is 1, i.e. no decreasing.
        param: connection: str, optional
            the way to combine two tensors, one of Branch_connection.
        param: another_task_flag: bool, optional
            indicating the index of layer which will generate data as input
            of another task.
        return: layer:
            layer instance.
        '''
        self.layers.append(layer)
        num = len(self.layers) - 1

        if layer_type:
            assert layer_type in Branch._layer_types

            if layer_type == 'down':
                assert connection in Branch._connection
                self.pairs.append((num, decrease_scale, connection))
            if layer_type == 'up':
                (_num, _decrease_scale, _connection) = self.pairs.pop()
                self.down_path[_num] = (_decrease_scale, _connection)
                self.up_path[num] = _num

        if another_task_flag:
            self.another_task_index = num

        return layer

    def _connect_branches(self, data1, data2, decrease_scale, connection):
        '''Branch connection.'''
        if decrease_scale != 1:
            data21 = Pool2d(acti='avg', filter_size=decrease_scale,
                filter_strides=decrease_scale)(data2)
            data22 = Pool2d(acti='max', filter_size=decrease_scale,
                filter_strides=decrease_scale)(data2)
            data2 = tf.concat([data21, data22], axis=-1)

        if connection == 'add':
            return BaseLayer._add(data1, data2)
        if connection == 'concat':
            return tf.concat([data1, data2], axis=-1)

    def __call__(self, data_flow):
        '''Forward.'''
        assert len(self.pairs) == 0, "down- and up-path are not sysmetric."
        branch_data = dict() # for storing data to be connected

        with tf.name_scope(self.name_scope):
            for index, layer in enumerate(self.layers):
                data_flow = layer(data_flow)
                self.collections.append(layer.__dict__)

                if index in self.down_path.keys():
                    branch_data[index] = {
                        'data_flow': data_flow,
                        'decrease_scale': self.down_path[index][0],
                        'connection': self.down_path[index][1]
                    }
                if index in self.up_path.keys():
                    _key = self.up_path[index]
                    data_flow = self._connect_branches(data_flow,
                        branch_data[_key]['data_flow'],
                        branch_data[_key]['decrease_scale'],
                        branch_data[_key]['connection'])

                data_flow = tf.verify_tensor_all_finite(data_flow,
                    "output of {}-{} is not finite".format(
                    self.name_scope, layer.name_scope))

                # another task
                if hasattr(self, 'another_task_index'):
                    if self.another_task_index == index:
                        another_output = data_flow
                else: another_output = None

        self._logger()
        if another_output is None: return data_flow
        else: return another_output, data_flow


import plotly.graph_objects as go
from .visualization_array import visualization_array
from .random_colors import random_colors
from igraph import Graph, EdgeSeq
from utils.utils import sorted_data, discrete_columns, cols_for_clustering

def visualize_combinations(sorted_cols, sorted_values, title):
    fig = go.Figure(go.Surface(
        contours={
            "x": {"show": True, "start": 1.5, "end": 2, "size": 0.04, "color": "white"},
            "z": {"show": True, "start": 0.5, "end": 0.8, "size": 0.05}
        },
        x=sorted_cols,

        y=sorted_cols,
        z=visualization_array(sorted_cols, sorted_values)
    )
    )
    fig.update_layout(
        title=title,
        scene={
            "xaxis": {"nticks": 20},
            "zaxis": {"nticks": 4},
            'camera_eye': {"x": 0, "y": -1, "z": 0.5},
            "aspectratio": {"x": 1, "y": 1, "z": 0.2}
        })
    fig.show()

def visualize_cols_stats(data):
    cat_cols = discrete_columns(data)
    cont_cols = cols_for_clustering(data)
    labels = ["Discrete columns", "Continuous columns"]
    values= [len(cat_cols), len(cont_cols)]
    fig = go.Figure(data=[go.Pie(labels=labels, values=values)])
    fig.show()

def visualize_columns(cols, values, title):
    fig = go.Figure(data=[go.Scatter(
        x=cols, y=values,
        mode='markers',
        marker_size=[60 for i in range(len(cols))],
        marker_color=random_colors(len(cols))
    )
    ]
    )

    fig.update_layout(
        title=title
    )
    fig.show()

def hierarchical_visualization(data, sorted_cols, nbr_patterns):

    from utils.utils import network_vis_array
    data=sorted_data(data, sorted_cols)

    nbr_vis_cols = nbr_patterns//4
    vis_cols = sorted_cols[:nbr_vis_cols]
    values_list=network_vis_array(data, vis_cols)

    G = build_network(values_list, nbr_patterns)
    node_trace, edge_trace = network_components(G)
    set_traces(G, node_trace)

    draw_network(edge_trace, node_trace)

def build_network(cols, nbr_nodes):

    l = cols

    import networkx as nx
    G = nx.Graph()

    for j in range(len(l[0])):
        if G.number_of_nodes() >= nbr_nodes:
            break

        for i in range(len(l)):
            if G.number_of_nodes() < 20 and not (l[i][j] in G.nodes()):
                G.add_node(l[i][j], pos=(i * 2, 10 - j))
                last = j
                if j > 0 and l[i][j - 1] in G.nodes():
                    G.add_edge(l[i][j], l[i][j - 1])
                if j > last:
                    G.add_edge(l[i][j], l[i][last])
        for i in range(len(l) - 1):
            if l[i][j] in G.nodes() and l[i + 1][j] in G.nodes():
                G.add_edge(l[i][j], l[i + 1][j])

    return G

def network_components(G):
    edge_x = []
    edge_y = []
    for edge in G.edges():
        x0, y0 = G.nodes[edge[0]]['pos']
        x1, y1 = G.nodes[edge[1]]['pos']
        edge_x.append(x0)
        edge_x.append(x1)
        edge_x.append(None)
        edge_y.append(y0)
        edge_y.append(y1)
        edge_y.append(None)

    edge_trace = go.Scatter(
        x=edge_x, y=edge_y,
        line=dict(width=3, color='#888'),
        hoverinfo='none',
        mode='lines')

    node_x = []
    node_y = []
    for node in G.nodes():
        x, y = G.nodes[node]['pos']
        node_x.append(x)
        node_y.append(y)

    node_trace = go.Scatter(
        x=node_x, y=node_y,
        mode='markers',
        hoverinfo='text',
        marker=dict(
            showscale=True,
            # colorscale options
            # 'Greys' | 'YlGnBu' | 'Greens' | 'YlOrRd' | 'Bluered' | 'RdBu' |
            # 'Reds' | 'Blues' | 'Picnic' | 'Rainbow' | 'Portland' | 'Jet' |
            # 'Hot' | 'Blackbody' | 'Earth' | 'Electric' | 'Viridis' |
            colorscale='Rainbow',
            reversescale=True,
            color=[],
            size=50,
            colorbar=dict(
                thickness=15,
                title='Hierarchy levels',
                xanchor='left',
                titleside='right'
            ),
            line_width=2))
    return node_trace, edge_trace

def set_traces(G, node_trace):
    node_adjacencies = []
    node_text = []
    for node, adjacencies in enumerate(G.adjacency()):
        node_adjacencies.append(len(adjacencies[1]))

    node_trace.marker.color = node_adjacencies
    node_trace.text = list(G.nodes)

def draw_network(edge_trace, node_trace):
    fig = go.Figure(data=[edge_trace, node_trace],
                    layout=go.Layout(
                        title='Hierachical visualization',
                        titlefont_size=20,
                        showlegend=False,
                        hovermode='closest',
                        margin=dict(b=20, l=5, r=5, t=40),

                        xaxis=dict(showgrid=False, zeroline=False, showticklabels=False),
                        yaxis=dict(showgrid=False, zeroline=False, showticklabels=False))
                    )
    fig.show()


import sublime
import sublime_plugin

import re
def init(label_type):
	globals()['label_type'] = label_type
	globals()['setting'] = sublime.load_settings('URIReplace.sublime-settings')
	frameworks = setting.get('frameworks')
	use_framework_name = setting.get('use_framework')
	#检查use_framework是否在frameworks中，不在则退出
	use_framework_exist = False
	for framework in frameworks:
		if framework == use_framework_name:
			use_framework_exist = True
			break
	if use_framework_exist == False:
		sublime.error_message("use_framework is not in the frameworks")
		return
	else:
		globals()['use_framework'] = frameworks[use_framework_name]

def url_replace(view, edit, regexps):
	#得到全部区域
	regions = view.find_all(regexps['find'])

	# 不能用“for region in regions”这种方法替换
	# 每次replace后每个匹配项的Region的起终位置不会动态计算
	# 如果replace使文档的长度变了replace对应的区域就乱了
	# 所以要使用Region生成selection，selection可以动态计算位置

	#得到的区域添加到selection
	view.selection.add_all(regions)
	def do_url_replace(matched):
		return matched.group(1)+use_framework['prefix']+use_framework[label_type+'_prefix_path']+matched.group(2)+use_framework['suffix']+matched.group(3)
	#进行替换
	for sel in view.sel():
		#得到选区的字符串
		sel_str = view.substr(sublime.Region(sel.a,sel.b))
		#判断是否需要替换
		if re.search(regexps['no_replace'], sel_str) == None:
			#需要替换，进行替换
			view.replace(edit, sel, re.sub(regexps['replace'],do_url_replace,sel_str,1))
	#清除所有selection
	view.selection.clear()

def create_regexps():
	regexps = {}
	regexps['find'] = setting.get('source_url')[label_type+'_regexp']['find']
	regexps['replace'] = re.sub("{ignore_start_path_regexp}", use_framework['ignore_start_path_regexp'], setting.get('source_url')[label_type+'_regexp']['replace'], count=0, flags=0)
	print(regexps['replace'])
	regexps['no_replace'] = use_framework['no_replace_regexp']
	return regexps

class UrCssCommand(sublime_plugin.TextCommand):
	def run(self, edit):
		init("css")
		regexps = create_regexps()
		url_replace(self.view, edit, regexps)

class UrJsCommand(sublime_plugin.TextCommand):
	def run(self, edit):
		init("js")
		regexps = create_regexps()
		url_replace(self.view, edit, regexps)

class UrImgCommand(sublime_plugin.TextCommand):
	def run(self, edit):
		init("img")
		regexps = create_regexps()
		url_replace(self.view, edit, regexps)


#calss header
class _BANDWIDTH():
	def __init__(self,):
		self.name = "BANDWIDTH"
		self.definitions = [u'a measurement of the amount of information that can be sent between computers, through a phone line, etc.: ', u'the range of frequencies used to send information over a distance using phone wires', u'in radio, the width of a particular waveband']

		self.parents = []
		self.childen = []
		self.properties = []
		self.jsondata = {}

		self.specie = 'nouns'

	def run(self, obj1 = [], obj2 = []):
		return self.jsondata


from mitmproxy.addons.server_side_events import ServerSideEvents
from mitmproxy.test import taddons
from mitmproxy.test.tflow import tflow

async def test_simple():
    s = ServerSideEvents()
    with taddons.context() as tctx:
        f = tflow(resp=True)
        f.response.headers["content-type"] = "text/event-stream"
        s.response(f)
        await tctx.master.await_log(
            "mitmproxy currently does not support server side events."
        )


import myokit
import myokit.pacing as pacing
import numpy as np
import matplotlib
import matplotlib.pyplot as pl
import myokit.lib.markov as markov
import pints
import argparse
import os
import sys

# Load project modules
sys.path.append(os.path.abspath(os.path.join('../', 'python')))
import cells
import data
import biomarkers

pl.rcParams['axes.axisbelow'] = True

# Check input arguments
parser = argparse.ArgumentParser(
    description='Plot model and experimental data')
parser.add_argument('--cell', type=int, default=2, metavar='N',
                    help='repeat number : 1, 2, 3, 4, 5, 6')
parser.add_argument('--model', type=str, default='wang', metavar='N',
                    help='which model to use')
parser.add_argument('--protocol', type=int, default=1, metavar='N',
                    help='which protocol is used to fit the data: 1 for staircase #1, 2 for sine wave')
parser.add_argument("--show", action='store_true',
                    help="whether to show figures instead of saving them",
                    default=False)
parser.add_argument('--params', type=int, default=1, metavar='N',
                    help='which params to use')
parser.add_argument('--figsize1', type=float, nargs='+', default=[3.5, 2], \
                    help='Figure size in x and y, e.g. --figsize1 2.5 3.5')
parser.add_argument('--figsize2', type=float, nargs='+', default=[2.5, 3.5], \
                    help='Figure size in x and y, e.g. --figsize2 2.5 3.5')
parser.add_argument("--grid", action='store_true',
                    help="whether to add grid to figures or not",
                    default=False)
args = parser.parse_args()

cell = args.cell

pr_steps = [
    (6000, 1000), #-140
    (26000, 1000), #-110
    (46000, 1000), #-80
    (66000, 1000), #-50
    (86000, 1000), #-20
    (106000, 1000), #10
    (126000, 1000), #40
]

pr_voltages = np.array([-140, -110, -80, -50, -20, 10, 40])

pr_steps_deact = [
    (6025, 7000), #-140
    (26025, 7000), #-110
    (46025, 7000), #-80
    (66025, 7000), #-50
]

pr_voltages_deact = np.array([-140, -110, -80, -50])

#
# Simple IKr test script
#

# Get model
p = myokit.load_protocol('../model-and-protocols/inactivation-ramp.mmt')

current = 'ikr.IKr'

ek = cells.ek(cell)

print('Reversal potential ' + str(ek) + ' mV')

if args.protocol == 1:
    protocol_str = 'staircase1'
else:
    protocol_str = 'sine-wave'

# Run simulation
dt = 0.1

fig1 = pl.figure(1, figsize=args.figsize1, dpi=200)
ax11 = fig1.add_subplot(1,2,1)
pl.title('Inactivation', fontsize=10)
ax11.set_xlabel('Voltage (mV)')
ax11.set_ylabel('Norm. current')
if args.grid:
    ax11.grid(True)
ax12 = fig1.add_subplot(1,2,2)
pl.title('Deactivation', fontsize=10)
ax12.set_xlabel('Voltage (mV)')
ax12.set_ylabel(r'$\tau$ (ms)')
if args.grid:
    ax12.grid(True)
ax12.set_yscale('log')

fig2 = pl.figure(2, figsize=args.figsize2, dpi=200)
ax21 = fig2.add_subplot(311)
[label.set_visible(False) for label in ax21.get_xticklabels()]
ax21.set_xlim([500, 1600])
if args.grid:
    ax21.grid(True)
ax21.set_ylabel( 'Voltage (mV)' )
ax22 = fig2.add_subplot(312)
[label.set_visible(False) for label in ax22.get_xticklabels()]
ax22.set_xlim([500, 1600])
if args.grid:
    ax22.grid(True)
ax22.set_ylabel( 'Predicted\ncurrent (nA)' )
ax23 = fig2.add_subplot(313, sharey=ax22)
ax23.set_xlim([500, 1600])
if args.grid:
    ax23.grid(True)
ax23.set_xlabel( 'Time (ms)' )
ax23.set_ylabel( 'Experimental\ncurrent (nA)' )

colors = ['orange', 'red', 'limegreen']
linestyles = ['solid', 'dashed', 'dashdot']

models = ['wang', 'wang-r6', 'wang-r8']
for n, model in enumerate(models):

    m = myokit.load_model('../model-and-protocols/' + model + '-ikr-markov.mmt')
    if model == 'mazhari':
        model_str = 'Mazhari'
        states = ['ikr.c2', 'ikr.c3', 'ikr.o', 'ikr.i']
    elif model == 'mazhari-reduced':
        model_str = 'Maz-red'
        states = ['ikr.c1', 'ikr.c3', 'ikr.o', 'ikr.i']
    elif model == 'wang':
        model_str = 'Wang'
        states = ['ikr.c2', 'ikr.c3', 'ikr.o', 'ikr.i']
    elif model == 'wang-r1':
        model_str = 'Wang-r1'
        states = ['ikr.c2', 'ikr.c3', 'ikr.o', 'ikr.i']
    elif model == 'wang-r2':
        model_str = 'Wang-r2'
        states = ['ikr.c3', 'ikr.o', 'ikr.i']
    elif model == 'wang-r3':
        model_str = 'Wang-r3'
        states = ['ikr.c3', 'ikr.o', 'ikr.i']
    elif model == 'wang-r4':
        model_str = 'Wang-r4'
        states = ['ikr.c3', 'ikr.o', 'ikr.i']
    elif model == 'wang-r5':
        model_str = 'Wang-r5'
        states = ['ikr.o', 'ikr.i']
    elif model == 'wang-r6':
        model_str = 'Wang-r6'
        states = ['ikr.o', 'ikr.i']
    elif model == 'wang-r7':
        model_str = 'Wang-r7'
        states = ['ikr.o', 'ikr.i']
    elif model == 'wang-r8':
        model_str = 'Wang-r8'
        states = ['ikr.o']
    else:
        pass
    n_params = int(m.get('misc.n_params').value())
    m = markov.convert_markov_models_to_full_ode_form(m)

    # Number of steps in pacing protocol
    n_steps = 7

    # Set steady state potential
    LJP = m.get('misc.LJP').value()
    ss_V = -80 - LJP

    x_found = np.loadtxt('../cmaesfits/' + model_str + '-model-fit-' + protocol_str + '-iid-noise-parameters-' + str(args.params) + '.txt', unpack=True)

    parameters = []
    for i in range(n_params):
        parameters.append('ikr.p'+str(i+1))

    d = ['engine.time', 'membrane.V', 'ikr.IKr']

    # Run simulation
    m.get('nernst.EK').set_rhs(ek)

    # print('Updating model to steady-state for ' + str(ss_V) + ' mV ')
    m.get('membrane.V').set_label('membrane_potential')

    mm = markov.LinearModel.from_component(m.get('ikr'))

    x = mm.steady_state(ss_V, x_found)
    for i in range(len(states)):
        m.get(states[i]).set_state_value(x[i])

    m.get('membrane.V').set_rhs('engine.pace - misc.LJP')

    s = myokit.Simulation(m, p)
    s.set_tolerance(1e-8, 1e-8)
    s.set_max_step_size(0.1)

    # Update model parameters
    for i in range(n_params):
        s.set_constant('ikr.p'+str(i+1), x_found[i])

    d = s.run(p.characteristic_time(), log_interval=dt, log=d)

    e = myokit.DataLog.load_csv('../data/SFU-data/Inactivation/inactivation-WT-cell-' + str(cell) + '.csv').npview()

    # Apply capacitance filtering for experiment and simulated data
    signals = [e.time(), e['current']]
    voltage = 'voltage' in e
    if voltage:
        signals.append(e['voltage'])
    signals = data.capacitance(p, dt, *signals)

    e = myokit.DataLog()
    e.set_time_key('time')
    e['time'] = signals[0]
    e['current'] = signals[1] / 1000
    if voltage:
        e['voltage'] = signals[2]

    signals2 = [d.time(), d['ikr.IKr'], d['membrane.V']]
    d = myokit.DataLog()
    d.set_time_key('time')
    d['time'] = signals2[0]
    d['current'] = signals2[1]
    d['voltage'] = signals2[2]

    # Create colormap for plotting
    cmap = matplotlib.cm.get_cmap('viridis')
    norm = matplotlib.colors.Normalize(0, n_steps)

    # Filtered simulated data
    d = d.npview()
    df = d.fold(2000)

    # Filtered experimental data
    e = e.npview()
    e = e.fold(2000)

    # Compute inactivation curve
    v1, g1, voltages1, peaks1 = biomarkers.steady_state_inactivation(pr_steps=pr_steps, pr_voltages=pr_voltages, log=d, erev=ek)
    v2, g2 = biomarkers.time_constant_of_deactivation(pr_steps_deact, pr_voltages_deact, log=d, erev=ek)

    g1n = g1 / np.max(g1)

    a = np.loadtxt('Biomarkers/inact_ss_exp_WT_cell' + str(cell) + '.txt', unpack=True)
    b = np.loadtxt('Biomarkers/inact_deact_exp_WT_cell' + str(cell) + '.txt', unpack=True)

    exp_ss_v = a[0]
    exp_ss_i = a[1]
    exp_ss_in = a[1] / np.max(a[1])

    exp_deact_v = b[0]
    exp_deact_i = b[1]

    ax11.plot(v1, g1n, linestyle=linestyles[n], color=colors[n], lw=1, label=model_str)
    if n == 0:
        ax11.scatter(exp_ss_v, exp_ss_in, s=36, facecolors='none', edgecolors='#1f77b4', marker='o', label='Expt.')
    ax12.plot(v2, g2, linestyle=linestyles[n], color=colors[n], lw=1)
    if n == 0:
        ax12.scatter(exp_deact_v, exp_deact_i, s=36, facecolors='none', edgecolors='#1f77b4', marker='o')

    for k in range(n_steps):
        if n == 0:
            ax21.plot(df.time(), df['voltage',k], color='#1f77b4', lw=1)
            ax23.plot(e.time(), e['current',k], color='#1f77b4', lw=1)
        ax22.plot(df.time(), df['current',k], color=colors[n], lw=1, linestyle=linestyles[n], label=model_str if k == 0 else '')

# ax11.legend(fontsize=8)
# ax22.legend(fontsize=8)
fig1.tight_layout()
fig2.tight_layout()

if args.show == True:
    pl.show()
else:
    filename1 = 'All_figures/Biomarkers-inactivation-models-compare-fit-' + protocol_str + '-iid-noise'
    filename2 = 'All_figures/Inactivation-models-compare-fit-' + protocol_str + '-iid-noise'
    fig1.savefig(filename1 + '.svg')
    fig2.savefig(filename2 + '.svg')


# uncompyle6 version 3.7.4
# Python bytecode 2.7 (62211)
# Decompiled from: Python 3.6.9 (default, Apr 18 2020, 01:56:04)
# [GCC 8.4.0]
# Embedded file name: build/bdist.macosx-10.12-intel/egg/edsd/logpeeker.py
# Compiled at: 2016-05-08 02:40:49
"""
edas-detector.logpeeker : peek a log file based on the timestamp, exclude those
information which are before the selected
"""
__author__ = 'Thomas Li <yanliang.lyl@alibaba-inc.com>'
__license__ = 'GNU License'
from edsd import __version__
import re, os, sys, types
from edsd.common import isodt2ts, todaystime, ensure_dir, EDAS_DIRECTORY

def move_patial_log(logfile, start, end=None):
    """Parse a given log file according the start time and end time(be default:
     None), move the filter log lines into a tmp file, and then return the temp
     file's path.
    """
    ensure_dir(EDAS_DIRECTORY)
    tmpname = os.path.join(EDAS_DIRECTORY, os.path.basename(logfile))
    start = isodt2ts(start) if isinstance(start, types.StringType) else start
    end = -1 if end is None else end
    end = isodt2ts(end) if isinstance(end, types.StringType) else end
    with open(tmpname, 'w') as (f):
        for x in filter_log(logfile, start, end):
            f.writelines(x)

    return tmpname

def filter_log(filename, start, end=-1):
    started = False
    with open(filename) as (f):
        for x in f.xreadlines():
            if end == -1:
                pass
            found, isbetween = between(x, start, end)
            if not started and not isbetween:
                continue
            if found and started and not isbetween:
                return
            if not found and started:
                yield x
                continue
            if found and isbetween and not started:
                started = True
            if found and isbetween:
                yield x

def between(l, start, end=-1):
    found, lt = timestamp_in_line(l)
    end = end if end > 0 else sys.maxint
    return (
     found, start <= lt <= end)

p_TIME = re.compile('(\\d{1,2}:\\d{1,2}:\\d{1,2})')
p_DATE = re.compile('(\\d{1,4}[_/-]\\d{1,2}[_/-]\\d{1,4})')

def timestamp_in_line(l):
    g = p_TIME.search(l)
    if g is None:
        return (False, 0)
    else:
        time = g.group(1)
        l = l[:g.end()]
        g = p_DATE.search(l)
        if g is None:
            return (False, todaystime(time))
        date = g.group(1)
        return (g.start() < 12, isodt2ts('%s %s' % (date, time)))


###################################################
# students/urls.py: Consists of list of urls of students app of SAMS-IIITS
#__author__ = "Sri Harsha Gajavalli"
#__copyright__ = "Copyright 2017, SE2017 Course"
#__Team__ = ["Sri Harsha Gajavalli", "Koushik Bharadwaj", "David Christie", "Likhith Lanka", "Sajas P", "Rajeev Reddy"]
#__license__ = "MIT"
#__version__ = "1.2"
#__maintainer__ = "Likhith Lanka"
#__email__ = "likhith.l15@iiits.in"
#__status__ = "Development"
####################################################

# -*- coding: utf-8 -*-
from django.conf.urls import url
from . import views

urlpatterns = [
        url(r'^$',views.dashboard,name='dashboard'),
        url(r'^viewattendance/', views.ViewAttendance, name='ViewAttendance'),
        url(r'^assgnsubstatus/', views.AssgnSubStatus, name='AssgnSubmissionStatus'),
        url(r'^register_course/', views.register_course, name='register_course'),
        url(r'^register/', views.register, name='register'),
        url(r'^addcourse/', views.register, name='register'),
        url(r'^DropCourse/', views.DropCourse, name='DropCourse'),
        url(r'^unregister/', views.unregister, name='unregister'),

]


"""
匹配大小写的MAC地址
"""
# coding=utf-8

import re

def isValidIp(ip):
    if re.match(r"^\s*\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\s*$", ip):
        return True
    return False

def isValidMac(mac: str):
    if re.match(r"^\s*([0-9a-fA-F]{2,2}:){5,5}[0-9a-fA-F]{2,2}\s*$", mac):
        return True
    return False

if __name__ == "__main__":
    assert isValidMac("BC:5F:F4:6B:3E:6F") is True
    assert isValidMac("BC:F:F4:6B:3E:6F") is False
    assert isValidIp("192.168.2.105") is True
    assert isValidIp(".168.2.105") is False


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

def binary_search(array, target):
    l = 0
    r = len(array) - 1
    while l + 1 < r:
        mid = (l + r) >> 1
        if array[mid] == target:
            return mid
        if array[mid] < target:
            l = mid
        else:
            r = mid
        print(l,r)
    if array[l] == target:
        return l
    return r

def binary_search_2(array, target):
    l = 0
    r = len(array) - 1
    while l < r:
        m = l + ((r - l) >> 1)
        if array[m] == target:
            return array[m]
        elif array[m] < target:
            l = m + 1
        else:
            r = m
    return array[l]

array = [0,1,2,3,4,5,6,7,8,9,10,11,12, 13]
# print(binary_search(array, 6.5))
# print(binary_search(array, 5))
print(binary_search_2(array, 5))


import json
import zmq
import os
import json
import pickle
import numpy as np
from pdb import set_trace as stop
from ast import literal_eval
from datetime import datetime as dt
from django.shortcuts import render
from django.http import HttpResponse
from django.views.decorators.csrf import csrf_protect, csrf_exempt
from django.shortcuts import render, render_to_response
from celery import chain

from mansi.utils.urls import MSIxURL
from nsapi.tasks import msi_exec_task
from nsapi.models import IntradayData, CeleryResultData
from mansi.backend.tst.system.ns_handle_requests import REQUEST_IDS

# min number of transactions for a ticker
# within a day for analysis
MIN_NR_TRANSACTIONS = 300

# URL_MSI_PREFIX = "https://samarkanov.info/mansi/d/"
PORT_BACKEND = 5555
PICKLE_STORE = "/var/log/nsapi/stores"
API_MAP_FILE = os.path.join(os.path.dirname(os.path.abspath(__file__)), "api.conf")
HOME_DIR_MSI = os.path.join("/home", "samarka", "mansi")
TEST_CONFIG_FILE = os.path.join(HOME_DIR_MSI, "utils", "conint.json")

# UTILITIES
class NSxAPIxEmptyReplyFromBackend(Exception):
    pass

def log(text):
    with open("/var/log/nsapi/django.log", "a+") as ifile:
        ifile.write("\n%s: %s" %(dt.now(), text))

def store(data):
    with open(os.path.join(PICKLE_STORE, "tmp.data"), 'wb') as fp:
        pickle.dump(data, fp)

# POST-PROCESS REPRESENTATION OF SERIES
class Transaq(object):

    def __init__(self, *args, **kwargs):
        self.vol = kwargs["vol"]
        date_ = kwargs["date"]
        year, month, day = date_[:4], date_[4:6], date_[6:8]
        dt_ = kwargs["datetime"]
        hour, minute, sec = dt_[:2], dt_[2:], "00"
        self.datetime = str("%s-%s-%s %s:%s:%s" \
                                %(year, month, day, hour, minute, sec))
        # self.datetime = str(datetime_)
        self.high = kwargs["high"]
        self.low = kwargs["low"]
        self.close = kwargs["close"]
        self.open = kwargs["open"]

    def get_properties(self):
        items = []
        for item, value in self.__dict__.iteritems():
            if item[0] is not "_":
                items.append(item)
        return items

    @staticmethod
    def get_all(transacs, param):
        return [item.__dict__[param] for item in transacs]

    def __repr__(self):
        return "{vol: %s, datetime: %s, high: %s, low: %s, close: %s, open: %s}" \
                    %(self.vol, self.datetime, self.high, self.low, self.close, self.open)

# BACK-END HANDLER
class BackendHandler(object):

    def __init__(self, type_request):
        self.res = {"data":{}}
        self.type_request = type_request # string any of these two: (raw, ui)
        self._urlobj = {"errormsg": None, "url": None}

    def ask_backend(self, url=None):
        if url:
            self._urlobj["url"] = url
        if self._urlobj["url"]:
            context = zmq.Context()
            socket = context.socket(zmq.REQ)
            socket.connect("tcp://localhost:%i" %PORT_BACKEND)
            socket.send_string(self._urlobj["url"])
            log("about to send the url=%s to the backend" %self._urlobj["url"])
            # stop()
            try:
                rawdata = socket.recv().decode("utf-8")
                message = json.loads(rawdata)
            except ValueError:
                # print("URL: %s, data returned: %s" %(self._urlobj["url"], rawdata))
                # in case no data returned, then return nothing
                return dict()
            # if user asks raw json data, then return it immediately as
            # no need to post process it. Postprocesing is only needed
            # for POST request (so in case of UI request)
            if self.type_request == "raw":
                return message
            else:
                date_ = message["date"]
                transacs = [Transaq(date=date_, **item) for item in message["data"]]
                try:
                    items = transacs[0].get_properties()
                except IndexError:
                    raise NSxAPIxEmptyReplyFromBackend
                log("items: %s" %(items))
                for item in items:
                    # self OHLC and volume, datetime:
                    self.res["data"][item] = Transaq.get_all(transacs, item)
        else:
            self.res["data"]["errormsg"] = self._urlobj["errormsg"]
        return self.res

    def compose_url(self, request):
        if self.type_request == "raw":
            self._urlobj["url"] = request.urlencode()

        elif self.type_request == "ui":
            # request must be parsed and url string
            # to be composed out of the request
            ticker_ = request.get("ticker")
            datefrom_ = request.get("datefrom")
            dateto_ = request.get("dateto")
            step_ = request.get("step")
            if not ticker_ or not datefrom_ or not dateto_:
                self._urlobj["errormsg"] = "followind fields must be provided: "
                if not ticker_: self._urlobj["errormsg"] += "ticker, "
                if not datefrom_: self._urlobj["errormsg"] += "start date, "
                if not dateto_: self._urlobj["errormsg"] += "end date, "
                self._urlobj["errormsg"] = self._urlobj["errormsg"][:-2]
            else:
                # handle market type
                url_ =  "q=tradedata&"

                # handle date
                try:
                    dtfrom = dt.strptime(datefrom_, "%Y%m%d")
                    dtto = dt.strptime(dateto_, "%Y%m%d")
                except ValueError:
                    self._urlobj["errormsg"] = "wrongly formated date string (expected value: <Year><Month><Day>, example: 20170501)"
                    return self

                if dtfrom == dtto:
                    url_ += "date=%s&" %datefrom_
                else:
                    # check the input
                    if dtto < dtfrom:
                        self._urlobj["errormsg"] = "end day must be later than start day"
                        return self
                    url_ += "datefrom=%s&" %datefrom_
                    url_ += "dateto=%s&" %dateto_

                    # handle step (optional)
                    url_ += "step=%s" %step_

                # handle ticker
                url_ += "ticker=%s" %ticker_

                # return url
                self._urlobj["url"] = url_
        else:
            raise NotImplementedError
        return self

# VIEW FUNCTIONS
@csrf_exempt
def mansi(request):
    result = {"result": "here"}
    if request.method == "POST":
        reply = ""
        res = mansi_run(request.POST.get("code"))
        for item in res:
            if item: reply += "%s\n" %item
        result["result"] = reply
        return HttpResponse(json.dumps(result), content_type="application/json")

@csrf_exempt
def api_ui(request):
    # if request.method == "POST":
    #     bckend = BackendHandler(type_request="ui")
    #     reply = bckend.compose_url(request.POST).ask_backend()
    #     # store(reply)
    #     # reply = decorate_with_moving_average(reply)
    #     # post processing of data: calc of profit
    #     if reply and len(reply["data"]["vol"]) > MIN_NR_TRANSACTIONS:
    #         backtest = BackTester(strategy=Strategy1, data=reply)
    #         reply["data"]["profit_data"] = backtest.calc_profit()
    #         reply = backtest.decorate_with_moving_average(reply)
    #     return HttpResponse(json.dumps(reply), content_type="application/json")
    return render_to_response("nsindex.html", {})

def api_raw(request):
    bckend = BackendHandler(type_request="raw")
    reply = bckend.compose_url(request.GET).ask_backend()
    return HttpResponse(json.dumps(reply), content_type="application/json")

def api_raw_django(request):
    res = {}
    # log("api_raw_django: request.GET.date: %s" %request.GET.get("date"))
    # log("api_raw_django: request.GET.ticker: %s" %request.GET.get("ticker"))
    date = dt.strptime(request.GET.get("date"), "%Y%m%d").strftime("%Y-%m-%d")
    ticker = request.GET.get("ticker")
    series = IntradayData.objects.filter(ticker__abbrev=ticker, date=date)
    # log("api_raw_django: series: %s" %series)
    res["date"] = request.GET.get("date")
    res["data"] = []
    for item in series:
        ser = {}
        ser["vol"] = int(item.data.vol)
        ser["close"] = item.data.close
        ser["high"] = item.data.high
        ser["open"] = item.data.open
        ser["low"] = item.data.low
        ser["datetime"] = item.time.strftime("%H:%M:%S")
        res["data"].append(ser)
    return HttpResponse(json.dumps(res), content_type="application/json")

def make_command_str(idir, tstname):
    if ".py" in tstname:
        command = "python3 %s" %os.path.join(HOME_DIR_MSI, idir, tstname)
    else:
        command = os.path.join(HOME_DIR_MSI, idir, tstname)
    return command

@csrf_exempt
def testarena(request):
    res = {}

    if request.method == "POST":
        # user clicked button "retest"
        # clean cached celery data
        CeleryResultData.objects.all().delete()

    # Get test config
    with open(TEST_CONFIG_FILE, "r") as ifile:
        config = json.loads(ifile.read())

    for name, val in config.items():
        for tst in val:

            command = make_command_str(name, tst)
            res[command] = {}

            cached_obj = CeleryResultData.objects.filter(func=command)

            if cached_obj:
                log("DMSA cached_obj exists!")
                ob = cached_obj[0]
                # TODO: if results are too old then redo the execution
            else:
                # exec task and cache results (using DB backend)
                ob = CeleryResultData.objects.create(func=command, state="PENDING")
                msi_exec_task.apply_async((command,))

            res[command]["res"] = ob.res
            res[command]["date"] = str(ob.date)
            res[command]["time"] = str(ob.time)
            res[command]["tstdata"] = ob.data
            res[command]["state"] = ob.state

    return render_to_response("nstst.html", {"result": res})

@csrf_exempt
def listapis(request):
    res = {}
    res["APIs for charting"] = []
    res["General APIs"] = []
    res["Other APIs"] = []

    for idd, url in REQUEST_IDS.items():
        if "chartdata" in url["url"]:
            res["APIs for charting"].append(url["url"])
        elif "tradedata" in url["url"]:
            res["General APIs"].append(url["url"])
        else:
             res["Other APIs"].append(url["url"])
    return render_to_response("nsapis.html", {"result": res})
    # return HttpResponse(json.dumps(res), content_type="application/json")


T = int(input())
for tc in range(1, T+1):
    N, V = input().split()
    result = ''
    for num in V:
        num = int(num, 16)
        for i in range(3, -1, -1):
            if num & (1 << i):
                result += '1'
            else:
                result += '0'
    print('#{} {}'.format(tc, result))


# 1 - Reading and writing files

# Open a file for writing and create it if it doesn't exist
# + = Datei erstellen falls schon existiert
f = open("textfile.txt", "w+")

# write some lines of data to the file
for i in range(10):
    f.write("This is line " + str(i) + "\r\n")

# close the file when done
f.close()

# Open the file back up and read the contents
f = open("textfile.txt", "r")

if f.mode == "r":
    #wird ganze Datei ausgelesen
    contents = f.read()
    print(contents)
f.close()

f = open("textfile.txt", "r")
if f.mode == 'r':
    fl = f.readlines()
    for x in fl:
        print(x)
f.close()
# Open the file for appending text to the end
f = open("textfile.txt", "a")
for i in range(10):
    f.write("This is line " + str(i) + "\r\n")
f.close()

# 2 - Working with OS path utilities

#Klassen um mit Verzeichnisse und OS zu arbeiten
import os
from os import path
import datetime
from datetime import date, time, timedelta
import time

# Print the name of the OS
print(os.name)

# Check for item existence and type
print("Item exists: " + str(path.exists("textfile.txt")))
print ("Item is a file " + str(path.isfile("textfile.txt")))
print("Item is a directory " + str(path.isdir("textfile.txt")))
# Work with file paths
print ("Item path: " + str(path.realpath("textfile.txt")))
#trennt Pfad und Dateiname
print("Item path and name: " + str(path.split(path.realpath("textfile.txt"))))

# Get the modification time
#path.getmtime()  = gibt das Änderungsdatum zurück
t = time.ctime(path.getmtime("textfile.txt"))
print(t)
print(datetime.datetime.fromtimestamp(path.getmtime("textfile.txt")))
# Calculate how long ago the item was modified
td = datetime.datetime.now() - datetime.datetime.fromtimestamp(path.getmtime("textfile.txt"))
print("It has been " + str(td) + " since the file was modified")
print("Or, " + str(td.total_seconds()) + "seconds")

# 3 - Using file system shell methods
# = Dateien manipuliren mit Hilfe der Shell-Utilities
from os import path
import shutil
# make a duplicate of an existing file
if path.exists("textfile.txt"):
    # get the path to the file in the current directory
    src = path.realpath("textfile.txt")
    # let's make a backup copy by appending "bak" to the name
    dst = src + ".bak"
    #kopiert nur Inhalt der Datei, Dateieigenschaften nicht
    shutil.copy(src, dst)

    # copy over the permissions, modification times, and other info
    #kopiert Inhalt + Dateieigenschaften
    shutil.copystat(src, dst)

    # rename the original file
    os.rename("textfile.txt", "newfile.txt")
    os.rename("newfile.txt", "textfile.txt")

    # now put things into a ZIP archive
    from shutil import make_archive
    root_dir, tail = path.split(src)
    shutil.make_archive("arhive", "zip", root_dir)

# more fine-grained control over ZIP files
    from zipfile import ZipFile
    #Konstruktor aufruf für ZipFile() + Name des Objket is dann newzip
    with ZipFile("testzip.zip", "w") as newzip:
        newzip.write("textfile.txt")
        newzip.write("textfile.txt.bak")

ONLINE-KURSE/Become a Python Developer/3 - Learning Python/code-training/Code der Kapitel/4 - Working with Files/working_with_files-1.py


def func21():
    arr1 = list(map(int, input()[1:-1].split(",")))
    arr2 = list(map(int, input()[1:-1].split(",")))
    bucket = []
    for i in range(1001):
        bucket.append(0)
    for i in range(len(arr1)):
        bucket[arr1[i]] += 1
    index = 0
    for i in range(len(arr2)):
        while bucket[arr2[i]] > 0:
            bucket[arr2[i]] -= 1
            arr1[index] = arr2[i]
            index += 1
    for i in range(len(bucket)):
        while bucket[i] > 0:
            bucket[i] -= 1
            arr1[index] = i
            index += 1
    print(arr1)
    return
func21()


__author__ = "TheComet"

from Updateable import Updateable
import Config
import pygame

DTHROW_ADVANTAGE = 18

class PlatformBridgesHitstun(Updateable):
    def __init__(self, calc):
        self.calc = calc

        self.font = pygame.font.Font("sans.ttf", 18)
        self.text = self.font.render("Hitstun", True, (255, 255, 255))

    def draw(self, surface):
        # Draw horizontal line
        start = (Config.OFFSETX2, Config.OFFSETY2 + 100)
        end = (Config.OFFSETX2 + self.calc.uair3_end * Config.SCALEX2, Config.OFFSETY2 + 100)
        pygame.draw.line(surface, Config.TRAJECTORY_COLOR, start, end, 1)

        # "hitstun" text
        rect = self.text.get_rect()
        rect.left = Config.OFFSETX2
        rect.bottom = Config.OFFSETY2 + 100
        surface.blit(self.text, rect)

        self.draw_hit(surface, 0, DTHROW_ADVANTAGE, self.calc.hit1)
        self.draw_hit(surface, self.calc.hit1, self.calc.hit1_end, self.calc.hit2)
        self.draw_hit(surface, self.calc.hit2, self.calc.hit2_end, self.calc.hit3)

    def draw_hit(self, surface, hit_start, hit_end, next_hit_start):
        if next_hit_start < hit_end:
            color = (100, 255, 100)
        elif next_hit_start > hit_end:
            color = (255, 100, 100)
        else:
            color = (255, 255, 100)

        hit = hit_end if hit_end < next_hit_start else next_hit_start
        start = (hit_start * Config.SCALEX2 + Config.OFFSETX2, Config.OFFSETY2 + 100)
        end = (hit * Config.SCALEX2 + Config.OFFSETX2, Config.OFFSETY2 + 100)
        pygame.draw.line(surface, color, start, end, 1)

        start = (end[0], self.calc.get_height_at_frame(hit) * Config.SCALEY2 + Config.OFFSETY2 - 10)
        end = (end[0], end[1] + 10)
        pygame.draw.line(surface, Config.TRAJECTORY_COLOR, start, end, 1)
        text = self.font.render(f"{next_hit_start - hit_end}", True, color)
        rect = text.get_rect()
        rect.top = Config.OFFSETY2 + 100 + 2
        rect.left = hit * Config.SCALEX2 + Config.OFFSETX2 + 5
        surface.blit(text, rect)


#!/usr/bin/env python3

import sys
import os

def print_help():
    print("usage : ./MakeCaveManGraph n_nodes n_communities graphfile")

if len(sys.argv) != 4:
    print_help()
else:
    os.system("CaveManGenerator/caveman " + sys.argv[1] + " " + sys.argv[2] + " > tmp")

    graphfile = open(sys.argv[3], 'w')
    graphfile.write(sys.argv[1] + "\n")

    tmpfile = open('tmp', 'r')
    tmplines = tmpfile.readlines()
    for line in tmplines:
        graphfile.write(line)
    tmpfile.close()
    graphfile.close()

    os.system("rm tmp")


# -*- coding: utf-8 -*-
"""
Utilities for general EDGAR website and ftp tasks.

index URL: http://www.sec.gov/Archives/edgar/data/2098/0001026608-05-000015-index.htm
complete submission URL: http://www.sec.gov/Archives/edgar/data/2098/000102660805000015/0001026608-05-000015.txt
Exhibit/form URL: http://www.sec.gov/Archives/edgar/data/2098/000102660815000007/acu_10k123114.htm

EDGAR FTP change in 2016:
<2016 ftp URL: ftp://ftp.sec.gov/edgar/data/2098/0000002098-96-000003.txt
>2016 http URL: https://www.sec.gov/Archives/edgar/data/2098/0000002098-96-000003.txt

EDGAR HTML specification: https://www.sec.gov/info/edgar/ednews/edhtml.htm

:copyright: © 2018 by Mac Gaulin
:license: MIT, see LICENSE for more details.
"""
import re
# import datetime as dt

def parse_url(url):
    """Return CIK and Accession from an EDGAR HTTP or FTP url.

    :param string url: URL from EDGAR website or FTP server.

    :return: Tuple of strings in the form (cik, accession) or (None, None)
    :rtype: tuple (string/None, string/None)
    """
    url_re = re.compile(r'/(?P<cik>\d{1,10})/'
                        r'(?P<accession>\d{10}-?\d\d-?\d{6})', re.I)
    res = url_re.search(url)
    if res:
        acc = res.group('accession')
        if len(acc) == 18:
            acc = acc[:10] + '-' + acc[10:12] + '-' + acc[12:]
        return res.group('cik'), acc
    return None, None

def get_edgar_urls(cik, accession=None):
    """Generage URLs for EDGAR 'bulk download' and 'user facing' sites.
    `cik` parameter can be object with `cik` and `accession` attributes or keys.

    :param string,object cik: String CIK, or object with cik and accession attributes or keys.
    :param string accession: String ACCESSION number, or None if accession in CIK object.

    :return: Tuple of strings in the form (bulk DL url, user website url)
    :rtype: tuple (string, string)
    """
    if hasattr(cik, 'cik') and hasattr(cik, 'accession'): # cik is callable.
        accession = cik.accession if not accession else accession
        cik = cik.cik
    elif hasattr(cik, 'get') and cik.get('cik', None) and cik.get('accession', None):
        accession = cik.get('accession') if not accession else accession
        cik = cik.get('cik')

    # Before FPT change: ftp://ftp.sec.gov/edgar/
    return ('https://www.sec.gov/Archives/edgar/data/{}/{}.txt'.format(cik, accession),
            'http://www.sec.gov/Archives/edgar/data/{}/{}-index.htm'.format(cik, accession))

def edgar_links(cik, accession=None):
    """Generage HTML encoded links (using `a` tag) to EDGAR 'bulk download' and 'user facing' sites.
    `cik` parameter can be object with `cik` and `accession` attributes or keys.

    :param string,object cik: String CIK, or object with cik and accession attributes or keys.
    :param string accession: String ACCESSION number, or None if accession in CIK object.

    :return: String of HTTP encoded links
    :rtype: string
    """
    urls = get_edgar_urls(cik, accession=accession)

    return ("Raw Web"
            .format(*urls))

def _get_qtr(datetime_in):
    """
    Return the quarter (1-4) based on the month.
    Input is either a datetime object (or object with month attribute) or the month (1-12).
    """
    try:
        return int((datetime_in.month-1)/3)+1
    except AttributeError:
        pass
    try:
        return int((datetime_in-1)/3)+1
    except:
        raise

def get_feed_path(date):
    """Get URL path to daily feed gzip file."""
    feed_path = "/edgar/Feed/{0:%Y}/QTR{1}/{0:%Y%m%d}.nc.tar.gz"
    return feed_path.format(date, _get_qtr(date))

def get_idx_path(date_or_year, quarter=None, tar=False):
    """
    Get URL path to quarterly index file. Don't feed it a year and no quarter.
    """
    if quarter is None:
        quarter = _get_qtr(date_or_year)
    try:
        date_or_year = date_or_year.year
    except AttributeError:
        # Then date_or_year is an integer. Leave it be.
        pass
    idx_path = "/edgar/full-index/{0}/QTR{1}/master.idx{2}"
    return idx_path.format(date_or_year, quarter, '.gz' if tar else '')


#!/usr/bin/env python3
import sys
import traceback
from select import *
from socket import *
import time
import random

import re

sys.path.append("../lib")       # for params
import params

switchesVarDefaults = (
    (('-l', '--listenPort') ,'listenPort', 50000),
    (('-s', '--server'), 'server', "127.0.0.1:50001"),
    (('-d', '--debug'), "debug", False), # boolean (set if present)
    (('-?', '--usage'), "usage", False), # boolean (set if present)
    (('-p', '--pausedelay'), 'pauseDelay', 0.5)
    )

progname = "stammerProxy"
paramMap = params.parseParams(switchesVarDefaults)

server, listenPort, usage, debug, pauseDelay = paramMap["server"], paramMap["listenPort"], paramMap["usage"], paramMap["debug"], float(paramMap["pauseDelay"])

if usage:
    params.usage()

try:
    serverHost, serverPort = re.split(":", server)
    serverPort = int(serverPort)
except:
    print("Can't parse server:port from '%s'" % server)
    sys.exit(1)

try:
    listenPort = int(listenPort)
except:
    print("Can't parse listen port from %s" % listenPort)
    sys.exit(1)

print ("%s: listening on %s, will forward to %s\n" %
       (progname, listenPort, server))

sockNames = {}               # from socket to name
nextConnectionNumber = 0     # each connection is assigned a unique id

now = time.time()

class Fwd:
    def __init__(self, conn, inSock, outSock, bufCap = 1000):
        global now
        self.conn, self.inSock, self.outSock, self.bufCap = conn, inSock, outSock, bufCap
        self.inClosed, self.buf = 0, bytes(0)
        self.delaySendUntil = 0 # no delay
    def checkRead(self):
        if len(self.buf) < self.bufCap and not self.inClosed:
            return self.inSock
        else:
            return None
    def checkWrite(self):
        if len(self.buf) > 0 and now >= self.delaySendUntil:
            return self.outSock
        else:
            return None
    def doRecv(self):
        try:
            b = self.inSock.recv(self.bufCap - len(self.buf))
        except:
            self.conn.die()
            return
        if len(b):
            self.buf += b
        else:
            self.inClosed = 1
        self.checkDone()
    def doSend(self):
        global now
        try:
            bufLen = len(self.buf)
            toSend = random.randrange(1, bufLen+1)
            if debug: print("attempting to send %d of %d" % (toSend, len(self.buf)))
            n = self.outSock.send(self.buf[0:toSend])
            self.buf = self.buf[n:]
            if len(self.buf):
                self.delaySendUntil = now + pauseDelay
        except Exception as e:
            print(e)
            self.conn.die()
        self.checkDone()
    def checkDone(self):
        if len(self.buf) == 0 and self.inClosed:
            self.outSock.shutdown(SHUT_WR)
            self.conn.fwdDone(self)

connections = set()

class Conn:
    def __init__(self, csock, caddr, af, socktype, saddr):
        global nextConnectionNumber
        self.csock = csock      # to client
        self.caddr, self.saddr = caddr, saddr # addresses
        self.connIndex = connIndex = nextConnectionNumber
        nextConnectionNumber += 1
        self.ssock = ssock = socket(af, socktype)
        self.forwarders = forwarders = set()
        print("New connection #%d from %s" % (connIndex, repr(caddr)))
        sockNames[csock] = "C%d:ToClient" % connIndex
        sockNames[ssock] = "C%d:ToServer" % connIndex
        ssock.setblocking(False)
        ssock.connect_ex(saddr)
        forwarders.add(Fwd(self, csock, ssock))
        forwarders.add(Fwd(self, ssock, csock))
        connections.add(self)
    def fwdDone(self, forwarder):
        forwarders = self.forwarders
        forwarders.remove(forwarder)
        print("forwarder %s ==> %s from connection %d shutting down" % (sockNames[forwarder.inSock], sockNames[forwarder.outSock], self.connIndex))
        if len(forwarders) == 0:
            self.die()
    def die(self):
        print("connection %d shutting down" % self.connIndex)
        for s in self.ssock, self.csock:
            del sockNames[s]
            try:
                s.close()
            except:
                pass
        connections.remove(self)
    def doErr(self):
        print("forwarder from client %s failing due to error" % repr(self.caddr))
        die()

class Listener:
    def __init__(self, bindaddr, saddr, addrFamily=AF_INET, socktype=SOCK_STREAM): # saddr is address of server
        self.bindaddr, self.saddr = bindaddr, saddr
        self.addrFamily, self.socktype = addrFamily, socktype
        self.lsock = lsock = socket(addrFamily, socktype)
        sockNames[lsock] = "listener"
        lsock.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)
        lsock.bind(bindaddr)
        lsock.setblocking(False)
        lsock.listen(2)
    def doRecv(self):
        try:
            csock, caddr = self.lsock.accept() # socket connected to client
            conn = Conn(csock, caddr, self.addrFamily, self.socktype, self.saddr)
        except:
            print("weird.  listener readable but can't accept!")
            traceback.print_exc(file=sys.stdout)
    def doErr(self):
        print("listener socket failed!!!!!")
        sys.exit(2)

    def checkRead(self):
        return self.lsock
    def checkWrite(self):
        return None
    def checkErr(self):
        return self.lsock

l = Listener(("0.0.0.0", listenPort), (serverHost, serverPort))

def lookupSocknames(socks):
    return [ sockName(s) for s in socks ]

while 1:
    rmap,wmap,xmap = {},{},{}   # socket:object mappings for select
    xmap[l.checkErr()] = l
    rmap[l.checkRead()] = l
    now = time.time()
    nextDelayUntil = now + 10   # default 10s poll
    for conn in connections:
        for sock in conn.csock, conn.ssock:
            xmap[sock] = conn
            for fwd in conn.forwarders:
                sock = fwd.checkRead()
                if (sock): rmap[sock] = fwd
                sock = fwd.checkWrite()
                if (sock): wmap[sock] = fwd
                delayUntil = fwd.delaySendUntil
                if (delayUntil < nextDelayUntil and delayUntil > now): # minimum active delay
                    nextDelayUntil = delayUntil
    maxSleep = nextDelayUntil - now
    if debug: print("select max sleep=%fs" % maxSleep)
    rset, wset, xset = select(list(rmap.keys()), list(wmap.keys()), list(xmap.keys()), maxSleep)
    if debug: print([ repr([ sockNames[s] for s in sset]) for sset in [rset,wset,xset] ])
    for sock in rset:
        rmap[sock].doRecv()
    for sock in wset:
        wmap[sock].doSend()
    for sock in xset:
        xmap[sock].doErr()